【问题标题】:Mixing UIView animation and CGAffineTransformMakeRotation混合 UIView 动画和 CGAffineTransformMakeRotation
【发布时间】:2012-10-10 17:00:15
【问题描述】:

在阅读了一些关于动画的教程后,我正在测试一些东西。我用故事板创建了一个单视图 ARC 项目。我在主视图中添加了一个 UIImageView,中心位于 (200, 200)。我正确设置了插座。

我的想法是让图像 (UIImageView *bug) 向左移动,然后向右旋转 180°,向右移动,向左旋转 180°,以此类推。

这是我的代码

viewcontroller.h

@interface ViewController : UIViewController

@property (nonatomic, assign) IBOutlet UIImageView *bug;
@end

viewcontroller.m

#import "ViewController.h"
#import <CoreGraphics/CoreGraphics.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize bug;

#pragma mark - Animations


- (void)moveToLeft
{

    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         NSLog(@"Animation 1");
                         bug.center = CGPointMake(100, 200);
                     }
                     completion:^(BOOL finished){
                        [self turnToRight];
                     }
     ];       
}

-(void)turnToRight
{
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         NSLog(@"Animation 2");
                         bug.transform = CGAffineTransformMakeRotation(M_PI);                     }
                     completion:^(BOOL finished){
                     [self moveToRight];
                     }
     ];
}

- (void)moveToRight
{

    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         NSLog(@"Animation 3");
                         bug.center = CGPointMake(200, 200);
                     }
                     completion:^(BOOL finished){
                         [self turnToLeft];
                     }
     ];
}

-(void)turnToLeft
{
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         NSLog(@"Animation 4");

                             bug.transform = CGAffineTransformMakeRotation(0.0);
                     }
                     completion:^(BOOL finished){
                         [self moveToLeft];
                     }
     ];
}

在第二次调用 CGAffineTransformMakeRotation 之前一切正常。图像向左,向右旋转,向右,然后问题!动画中断,bug 移动到第一个 CGAffineTransformMakeRotation 的位置,然后向左旋转,重复上述步骤。

我尝试了不同的方法,但似乎每次对 CGAffineTransform 的后续调用都没有考虑 UIImageView 的新位置,并且总是在它被调用的第一个位置执行转换。我检查了很多示例,代码似乎正确,我只是不明白为什么将 CGAffineTransformMakeRotation 与常规动画混合会导致这种行为。

【问题讨论】:

    标签: objective-c xcode animation uiview cgaffinetransform


    【解决方案1】:

    您应该使用UIViewAnimationOptionCurveEaseInOut 而不是UIViewAnimationCurveEaseInOut。它们来自不同的枚举,并且在使用块动画时始终使用名称中带有 Option 的那些。
    您指定的值为UIViewAnimationOptionLayoutSubviews,这可能会导致重定位。

    这是我在您的代码中发现的唯一问题。如果这不是解决方案,那么您发现了一个错误:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多