2017-06-29 67 views
0

当按下时更改位置。 UIButton * button = sender;如何更改动画按钮的位置

CGPoint position = button.frame.origin; 
[UIButton setAnimationDuration:1.0]; 

CGSize size = button.frame.size; 
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); 
+0

https://developer.apple.com/documentation/uikit/uiview/1622418-animate – RJiryes

+1

你领回答? –

回答

0

试试这个:

button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // initial frame 

[UIView animateWithDuration:2 animations:^{ 
      button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // new frame frame 

     } completion:^(BOOL finished) { 

     }]; 
2

试试这个:

[UIView animateWithDuration:1.0 animations:^{ 
    btn.frame = CGRectOffset(btn.frame, 0, 20); 
}]; 
0

你可以使用UIView类的动画块...

不管你更改此块内的框架,画布填充框架在 之间设置开始框架

button.frame= yourInitialFrame;

[UIView animateWithDuration:1.0 animations:^{ 
    // set the End Frame here 
       button.frame = CGRectMake(position.x + 
      80,position.y,size.width,size.height); // new frame frame 

      } completion:^(BOOL finished) { 

      }]; 

也看到这个see this更多的帮助

1

对于1.0秒

 CGPoint position = button.frame.origin; 
     CGSize size = button.frame.size; 

     -(IBAction)btnClick:(id)sender { 
       [UIView animateWithDuration:1.0 animations:^{ 
        button.frame = CGRectMake(position.x+80,position.y,size.width,size.height); 
       }]; 
     } 
0
[UIView animateWithDuration:1.0 animations:^{ 
    // set the End Frame here 
       button.frame = CGRectMake(position.x + 
      80,position.y,size.width,size.height); // new frame frame 

      } completion:^(BOOL finished) { 

      }]; 
0
 [UIView animateWithDuration:0.2 animations:^{ 
     CGRect rect = button.frame; 
     rect.origin.x = rect.origin.x + 80; 
     button.frame = rect; 
    } completion:^(BOOL finished) { 

    }]; 
+1

尽管此代码可能会回答问题,但提供有关如何解决问题和/或解决问题原因的其他上下文会提高答案的长期价值。 –