动画 UIView 调用方法

Posted

技术标签:

【中文标题】动画 UIView 调用方法【英文标题】:Animate UIView calling a method 【发布时间】:2013-03-25 18:03:25 【问题描述】:

我有一个应该启动动画的功能。三角形是用“drawrect”设计的,我在控制器中有一个按钮,当按下它时,它会调用“startTriangleAnimation”。

问题是“startTriagnleAnimation”方法没有添加动画。我确信该程序会进入此方法,因为它会打印 NSLOG。

有人知道怎么做吗?

- (void)startTriangleAnimation

    [self setNeedsDisplay];
    if (_leftFoot) 
            NSLog(@"LEFT FOOT ANIMATION STARTING");
        [UIView animateWithDuration:15
                              delay:0
                            options: UIViewAnimationOptionCurveLinear
                         animations:^
                             self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-FIRST_ROTATION));
                          completion:^(BOOL finished) 
                             [UIView animateWithDuration:15
                                                   delay:0.5
                                                 options: UIViewAnimationOptionCurveLinear
                                              animations:^
                                                  self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(-SECOND_ROTATION));
                                               completion:^(BOOL finished) 
                                                  [UIView animateWithDuration:15
                                                                        delay:0.5
                                                                      options: UIViewAnimationOptionCurveLinear
                                                                   animations:^
                                                                       self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(-THIRD_ROTATION));
                                                                   completion:^(BOOL finished) 
                                                                       [UIView animateWithDuration:15
                                                                                        animations:^
                                                                                            self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(-FOURTH_ROTATION));
                                                                                        ];
                                                                   ];
                                              ];
                         ];



     else 
         NSLog(@"RIGHT FOOT ANIMATION STARTING");
        [UIView animateWithDuration:15
                              delay:0
                            options: UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
                         animations:^
                             NSLog(@"animating");
                             self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(FIRST_ROTATION));
                          completion:^(BOOL finished) 
                             [UIView animateWithDuration:15
                                                   delay:0.5
                                                 options: UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
                                              animations:^
                                                  self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(SECOND_ROTATION));
                                               completion:^(BOOL finished) 
                                                  [UIView animateWithDuration:15
                                                                        delay:0.5
                                                                      options: UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState
                                                                   animations:^
                                                                       self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(THIRD_ROTATION));
                                                                   completion:^(BOOL finished) 
                                                                       [UIView animateWithDuration:15
                                                                                        animations:^
                                                                                            self.transform = CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(FOURTH_ROTATION));
                                                                                        ];
                                                                   ];
                                              ];
                         ];



    


- (void)drawRect:(CGRect)rect

    CGFloat x = self.bounds.size.height;
    CGFloat y = self.bounds.size.width;

    [[UIColor redColor] setStroke];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(x/2, y - 20)];
    [path addLineToPoint:CGPointMake(x/2 - 10, y)];
    [path addLineToPoint:CGPointMake(x/2 + 10, y)];
    [path closePath];
    [path fill];
    //[path stroke];

    if (_leftFoot) 
        self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(10));

     else 
        self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-10));


    




【问题讨论】:

那个代码缩进伤害了我的眼睛......和我的心 这是 xcode 标识 【参考方案1】:

一种可能的解释是您的 FIRST_ROTATION 等变量中有意外的值。

另一个是你在drawRect中旋转。在我看来,这会产生意想不到的后果。如果您希望您的“基础三角形”不是直的,只需为您的路径计算适当的点并立即绘制它。

【讨论】:

是的,但是如何从外部调用动画? 你没有从drawRect内部调用动画。

以上是关于动画 UIView 调用方法的主要内容,如果未能解决你的问题,请参考以下文章

UIView动画

动画中断时未调用 UIView 动画完成块

swift UIView 动画完成处理程序首先调用

UIView 核心动画在 viewdidLoad 方法中不起作用?

UIView核心动画无法在viewdidLoad方法中工作?

在 awakeFromNib 上调用 UIView animate withDuration 不会执行动画