如何将单击事件附加到ios中的动画按钮

时间:2012-03-01 14:18:50

标签: ios5 uibutton core-animation

我有一个可移动的按钮,并希望在用户点击该按钮时执行一个方法 - 这可能在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]; 

2 个答案:

答案 0 :(得分:5)

我发现您只需将 UIViewAnimationOptionAllowUserInteraction 选项添加到动画中即可处理动画和交互:

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

答案 1 :(得分: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];

}