2012-03-01 66 views
1

我有一個可移動的按鈕,並希望當用戶單擊該按鈕時執行一個方法 - 在ios中可能嗎?如何將點擊事件添加到ios中的動畫按鈕

我寫了下面的代碼,但動畫需要以完成到能夠觸發該功能:只要

[arrowButton addTarget:self action:@selector(presentMainWindow) forControlEvents:UIControlEventTouchUpInside]; 


[UIView animateWithDuration:kAnimationDuration 
          delay:.2f 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations: 
    ^{ 
     [arrowButton setTransform:CGAffineTransformMakeTranslation(kButtonShift, 0)]; 
     [arrowButton setFrame:CGRectMake(arrowButton.frame.origin.x + kButtonShift, arrowButton.frame.origin.y, arrowButton.frame.size.width, arrowButton.frame.size.height); 
    } 
        completion:nil]; 

回答

5

我發現,你可以選擇UIViewAnimationOptionAllowUserInteraction簡單地添加到動畫中同時處理動畫和交互:

[UIView animateWithDuration:kAnimationDuration 
          delay:.2f 
         options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction 
        animations: 
    ^{ 
     [arrowButton setTransform:CGAffineTransformMakeTranslation(kButtonShift, 0)]; 
    } 
        completion:nil]; 
+0

給我自己和其他人的提示:-)你可以凍結動畫,然後進行以下操作:[CATransaction begin]; [_arrowButton.layer removeAllAnimations]; [CATransaction commit]; – tiguero 2012-03-04 10:50:49

0

作爲動畫只有你能看到什麼效果 而不是可以觸摸你只能點擊按鈕,同時動畫,如果你使自己的動畫像這樣

我不知道這是最好的解決方案,但它是一個工作解決方案。

- (IBAction)startanimation:(id)sender { 

    old = CGPointMake(arrowButton.frame.origin.x,arrowButton.frame.origin.x); 
    [self ani]; 


} 
- (void)ani { 
    NSLog(@"ani1"); 

    if (arrowButton.frame.origin.x < old.x + kButtonShift) { 

     NSTimer*myTimer; 

     myTimer = [NSTimer scheduledTimerWithTimeInterval:kTimeInterval/kButtonShift*4 target:self selector:@selector(ani2) userInfo:nil repeats:NO]; 

} 
} 
- (void)ani2 { 
    NSLog(@"ani2"); 

    arrowButton.frame = CGRectMake((arrowButton.frame.origin.x + 1), arrowButton.frame.origin.y, arrowButton.frame.size.width, arrowButton.frame.size.height); 
    [self ani]; 

} 
+1

你的答案您好感謝!雖然它適用於你的方法,但我發現你可以在動畫中使用UIViewAnimationOptionAllowUserInteraction選項,所以實際上不需要寫你自己的動畫! – tiguero 2012-03-03 10:56:23

相關問題