藍懿 ios技術交流和心得分享12.28

- (void)viewDidLoad { [super viewDidLoad]; // 1.監聽通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField]; } //監聽結束需要移除- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

#import "TutorialProjectViewController.h"@implementation TutorialProjectViewController@synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;// ------ Tutorial code starts here ------- (IBAction) startThreaonPressed:(UIButton *)sender {        threadStartButton.hidden = YES;    threadValueLabel.text = @"0";    threadProgressView.progress = 0.0;    [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];    }- (void)startTheBackgroundJob {      //註冊一個監聽者    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Update) name:@"UpdateTableView" object:nil];      NSLog(@"1111111111");    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];    // wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time    [NSThread sleepForTimeInterval:3];    [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];    [pool release];    }- (void)makeMyProgressBarMoving {    xx++;//  NSLog(@"22222222 %i",xx);    float actual = [threadProgressView progress];    threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];    if (actual < 1) {        threadProgressView.progress = actual + 0.01;        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];     }    if (xx==5||xx==15) {                //發出一個消息。此前註冊的監聽者可以發現這個消息,並且出發相應的方法        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateTableView" object:nil];            NSLog(@"xxxxxxxxxxxxxx==3");    }    if (xx==10) {        //取消監聽。        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UpdateTableView" object:nil];    }    else threadStartButton.hidden = NO;    }//受到監聽的消息時,調用這個函數-(void)Update{    NSLog(@"----------sdafs");}- (IBAction) testValueSliderChanged:(UISlider *)sender {        testValueLabel.text = [NSString stringWithFormat:@"%.2f", sender.value];    }// ------ Tutorial code ends here ------- (void)didReceiveMemoryWarning {    // Releases the view if it doesn't have a superview.    [super didReceiveMemoryWarning];        // Release any cached data, images, etc that aren't in use.}- (void)viewDidUnload {    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}// This function is for button which takes you to the xprogress.com website- (IBAction) runXprogressComButton: (id) sender {    NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.xprogress.com/" ];    [[UIApplication sharedApplication] openURL:url];}- (void)dealloc {        // ------ Tutorial code starts here ------        [threadValueLabel release];    [threadProgressView release];    [threadStartButton release];        [testValueLabel release];        // ------ Tutorial code ends here ------        [super dealloc];}@end​

 

  • UITapGestureRecognizer
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UILongPressGestureRecognizer

從命名上不難了解這些類別所對應代表的手勢,分別是 Tap(點一下)、Pinch(二指往內或往外撥動)、Rotation(旋轉)、Swipe(滑動,快速移動)、Pan (拖移,慢速移動)以及 LongPress(長按)。這些手勢別在使用上也很簡單,只要在使用前定義並添加到對應的視圖上即可。

// 定義一個 recognizer, 並加到需要偵測該手勢的 UIView 元件上

-

 (void)viewDidLoad {

    UISwipeGestureRecognizer

*

 recognizer;

    // handleSwipeFrom 是偵測到手勢,所要呼叫的方法

    recognizer

 = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)];

    // 不同的 Recognizer 有不同的實體變數

    // 例如 SwipeGesture 可以指定方向

    // 而 TapGesture 則可以指定次數

    recognizer

.

direction

 = UISwipeGestureRecognizerDirectionUp

    [

self

.

view

 addGestureRecognizer:recognizer];

    [

recognizer

 release];

 }

 

 -

 (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {

    // 觸發手勢事件後,在這裏作些事情

    // 底下是刪除手勢的方法

    [

self

.

view

 removeGestureRecognizer:recognizer];

 }

問題來了。有些手勢其實是互相關聯的,例如 Tap 與 LongPress、Swipe與 Pan,或是 Tap 一次與Tap 兩次。當一個 UIView 同時添加兩個相關聯的手勢時,到底我這一下手指頭按的要算是 Tap 還是 LongPress?如果照預設作法來看,只要「先滿足條件」的就會跳出並呼叫對應方法,舉例來說,如果同時註冊了 Pan 和 Swipe,只要手指頭一移動就會觸發 Pan 然後跳出,因而永遠都不會發生 Swipe;單點與雙點的情形也是一樣,永遠都只會觸發單點,不會有雙點。

那麼這個問題有解嗎?答案是肯定的,

UIGestureRecognizer 

有個方法叫做

requireGestureRecognizerToFail

,他可以指定某一個 recognizer,即便自己已經滿足條件了,也不會立刻觸發,會等到該指定的 recognizer 確定失敗之後才觸發。以同時支持單點與雙點的手勢爲例,代碼如下:

-

 (void)viewDidLoad {

    // 單擊的 Recognizer

    UITapGestureRecognizer

*

 singleRecognizer;

    singleRecognizer

 = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];

    singleTapRecognizer

.

numberOfTapsRequired

 = 1; // 單擊

    [

self

.

view

 addGestureRecognizer:singleRecognizer];

    

    // 雙擊的 Recognizer

    UITapGestureRecognizer

*

 double;

    doubleRecognizer

 = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];

    doubleTapRecognizer

.

numberOfTapsRequired

 = 2; // 雙擊

    [

self

.

view

 addGestureRecognizer:doubleRecognizer];

    

    // 關鍵在這一行,如果雙擊確定偵測失敗才會觸發單擊

    [

singleRecognizer

 requireGestureRecognizerToFail:doubleRecognizer];

    [

singleRecognizer

 release];

    [

doubleRecognizer

 release];

}

學習ios  重要還是要理清楚思路  在做或者看老師代碼的時候 自己多想想爲什麼  不要自己看着就抄       另外還是要推薦一下 藍懿IOS這個培訓機構  和劉國斌老師劉國斌老師還是很有名氣的,聽朋友說劉老師成立了藍懿iOS,,老師講課方式很獨特,能夠儘量讓每個人都能弄明白,有的比較難懂的地方,如果有的地方還是不懂得話,老師會換個其它方法再講解,這對於我們這些學習iOS的同學是非常好的,多種方式的講解會理解得更全面,這個必須得給個贊,嘻嘻,還有就是這裏的學習環境很好,很安靜,可以很安心的學習,安靜的環境是學習的基礎,小班講課,每個班20幾個學生,學習氛圍非常好,每天都學到9點多才離開教室,練習的時間很充裕,而且如果在練習的過程中有什麼困難,隨時可以向老師求助,不像其它機構,通過視頻教學,有的甚至學完之後都看不到講師本人,問點問題都不方便,這就是藍懿與其它機構的區別,相信在劉國斌老師的細心指導下,每個藍懿學員都能找到滿意的工作,加油!

                                                                  寫博客第七十九天;

                                                                              QQ:565803433


  1.     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardShow:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardHide:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
    
    複製代碼
  2. 在鍵盤將要出現和隱藏的回調中,加入動畫。
    複製代碼
    - (void)keyboardWillShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        
        CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = rect.origin.y;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            
            CGFloat maxY = CGRectGetMaxY(sub.frame);
            if (maxY > y - 2) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    
    - (void)keyboardWillHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    
    複製代碼
  1.     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardShow:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardHide:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
    
    複製代碼
  2. 在鍵盤將要出現和隱藏的回調中,加入動畫。
    複製代碼
    - (void)keyboardWillShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        
        CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat y = rect.origin.y;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            
            CGFloat maxY = CGRectGetMaxY(sub.frame);
            if (maxY > y - 2) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardShow:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    
    - (void)keyboardWillHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.25];
        NSArray *subviews = [self subviews];
        for (UIView *sub in subviews) {
            if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
                sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
            }
        }
        [UIView commitAnimations];
    }
    
    - (void)keyboardHide:(NSNotification *)notif {
        if (self.hidden == YES) {
            return;
        }
    }
    
    複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章