UIButton的动画

问题描述:

我想创建一个UIButton,它在按下屏幕后随机移动屏幕。我想用块来使用动画。这是怎么回事?UIButton的动画

+0

从这里http://*.com/ a/1679402/932011 – akk 2012-04-16 09:02:20

+0

感谢您的链接,但我必须使用这些块。我怎样做? – 2012-04-16 09:08:41

+0

这个问题有什么问题?为什么倒票? – SomaMan 2012-04-16 11:21:33

试试这个,这是一个简单的例子,将50像素的动画按钮,然后右后卫试(别忘了IB链接为myButton) -

.H -

@property (nonatomic, retain) IBOutlet UIButton *myButton; 

.m -

@synthesize myButton; 

-(IBAction)myButtonPressed { 

    CGPoint oldCentre = myButton.center; 
    CGPoint newCentre = myButton.center; 
    newCentre=CGPointMake(newCentre.x + 50.0, newCentre.y); 

    [UIView animateWithDuration:0.3 
         delay:0.0 
        options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        myButton.center = newCentre; 
       } 
       completion:^(BOOL finished){ 

        [UIView animateWithDuration:0.3 
              delay:0.0 
             options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
             animations:^{ 
              myButton.center = oldCentre; 
             } 
             completion:nil 
         ]; 
       } 
    ]; 

} 

您可以在块中放入任何喜欢的动画。

你可能要考虑摆脱UIViewAnimationOptionAllowUserInteraction位作为可能会有一些有趣的结果,如果用户保留在动画期间触摸按钮...

+0

很高兴听到它:) – SomaMan 2012-04-16 09:51:35