如何用动画改变按钮的位置
Posted
技术标签:
【中文标题】如何用动画改变按钮的位置【英文标题】:how to change position of button with Animation 【发布时间】:2017-06-29 08:50:48 【问题描述】:按下时改变位置。 UIButton * 按钮 = 发送者;
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);
【问题讨论】:
developer.apple.com/documentation/uikit/uiview/1622418-animate 你得到答案了吗? 【参考方案1】:试试这个:
[UIView animateWithDuration:1.0 animations:^
btn.frame = CGRectOffset(btn.frame, 0, 20);
];
【讨论】:
【参考方案2】:用于动画UIButton
的位置,持续时间为 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);
];
【讨论】:
【参考方案3】:试试这个:
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)
];
【讨论】:
【参考方案4】:您可以使用 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 以获得更多帮助
【讨论】:
【参考方案5】:[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)
];
【讨论】:
【参考方案6】:试试这个:
-(IBAction)ButtonClick:(id)sender
UIButton *aBtnRef = sender;
[UIView animateWithDuration:0.5 animations:^
aBtnRef.frame = CGRectMake(aBtnRef.frame.origin.x + 50,aBtnRef.frame.origin.y, aBtnRef.frame.size.width, aBtnRef.frame.size.height);
completion:^(BOOL finished)
];
更多信息:
Animation : How to make UIbutton move from right to left when view is loaded?
Moving UIButton position, animated
【讨论】:
【参考方案7】: [UIView animateWithDuration:0.2 animations:^
CGRect rect = button.frame;
rect.origin.x = rect.origin.x + 80;
button.frame = rect;
completion:^(BOOL finished)
];
【讨论】:
虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高答案的长期价值。以上是关于如何用动画改变按钮的位置的主要内容,如果未能解决你的问题,请参考以下文章