问题描述
我在我的项目创造了一些动画。基本上,我使用的UIView动画和CGAffineTransform,但很奇怪的事情发生了,我不知道。希望有人能帮助我解决这个问题。先谢谢了。
这是奇怪的事情:
在用户点击按钮后,按钮滑出屏幕,另外两个按钮在屏幕上滑动(我只是改变了这些按钮的中心点来实现这个动画)。并且,一段时间后,在屏幕上的图开始摇动(我使用CGAffineTransform来实现这一点)。
就在这时,奇怪的事情发生了 - 这previous滑出屏幕在原来的位置再次出现了,另外两个按钮消失的按钮(不包括动画,只是显示和消失)
以下是相关code,
1)按钮关闭幻灯片和幻灯片动画相关code
- (IBAction为)开始:(ID)发送者
{
// 1.滑动的取消和暂停按钮
[UIView的animateWithDuration:0.5F动画:^ {
[startButton setCenter:CGPointMake(startButton.center.x + 300.0f,startButton.center.y)];
[cancelButton setCenter:CGPointMake(cancelButton.center.x + 300.0f,cancelButton.center.y)];
[暂停按钮setCenter:CGPointMake(pauseButton.center.x + 300.0f,pauseButton.center.y)];
}完成:^(BOOL完){
如果(成品){
的NSLog(@移动结束);
}
}];
}
2)摇动画相关code视图
- (无效)shakeView:(UIView的*)viewToShake
{
CGFloat的T = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity,T,0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0); viewToShake.transform = translateLeft; [UIView的animateWithDuration:0.07延迟:0.0选项:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat动画:^ {
[UIView的setAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
}完成:^(BOOL完){
如果(成品){
[UIView的animateWithDuration:0.05延迟:0.0选项:UIViewAnimationOptionBeginFromCurrentState动画:^ {
viewToShake.transform = CGAffineTransformIdentity;
}完成:无];
}
}];
}
这是不奇怪,这是一个常见的问题与
自动布局。如果通过更改帧移动UI元素,那么只要别的东西发生,需要铺设了意见,提出的意见将恢复到其约束所定义的位置。要修复它,你要么需要关闭自动布局,或改变做你的动画约束不帧。
I created some animations in my project. Basically, I use UIView animate and CGAffineTransform, but a very strange thing happened and I have no idea. Hope someone can help me solve this problem. Thanks in advance.
This is the strange thing:After the user clicks on a button, the button slides off screen and another two buttons slide on the screen (I just changed the center point of these buttons to achieve this animation). And, some time later, a view on the screen start shaking (I use CGAffineTransform to achieve this).
At this moment, the strange thing happens - the button that previous slid off screen show up at its original position again and the other two buttons disappear (No animation, just shows up and disappear).
The following is the related code,
1) Button slide off and slide in animation related code
- (IBAction)start:(id)sender
{
// 1. Slide in cancel and pause button
[UIView animateWithDuration:0.5f animations:^{
[startButton setCenter:CGPointMake(startButton.center.x + 300.0f, startButton.center.y)];
[cancelButton setCenter:CGPointMake(cancelButton.center.x + 300.0f, cancelButton.center.y)];
[pauseButton setCenter:CGPointMake(pauseButton.center.x + 300.0f, pauseButton.center.y)];
} completion:^(BOOL finished) {
if (finished) {
NSLog(@"Move finished");
}
}];
}
2) The view shaking animation related code
- (void)shakeView:(UIView *)viewToShake
{
CGFloat t = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:2.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:nil];
}
}];
}
This isn't weird, it's a common problem with auto layout. If you move UI elements by changing frames, then as soon as something else takes place that requires laying out views, the moved views will revert to the position defined by their constraints. To fix it, you either need to turn off auto layout, or do your animations by changing constraints not frames.
这篇关于使用的UIView动画和CGAffineTransform时怪异的行为发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!