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

tig*_*ero 1 core-animation uibutton ios5

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

tig*_*ero 5

我发现你可以简单地将选项UIViewAnimationOptionAllowUserInteraction添加到动画中来处理动画和交互:

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