【问题标题】:iOS: why object turns to the start position after animation comletioniOS:为什么动画完成后对象会转到开始位置
【发布时间】:2014-03-17 17:37:30
【问题描述】:

)) 我正在尝试对 UIButton 进行动画处理(两个并行动画:图像的变化和移动),如下所示,动画按钮应该移动后,但完成动画按钮后会转到开始位置。请帮忙解决!提前致谢!

- (IBAction)bugOneTapped:(id)sender
{
   [UIView animateWithDuration:1.0
                 animations:^{
                     self.bugOneButton.imageView.animationImages =
                     [NSArray arrayWithObjects:[UIImage imageNamed:@"bugOne3"],[UIImage imageNamed:@"bugOne2"],[UIImage imageNamed:@"bugOne1"],[UIImage imageNamed:@"bugOne2"],[UIImage imageNamed:@"bugOne3"],[UIImage imageNamed:@"bugOne4"],[UIImage imageNamed:@"bugOne5"],[UIImage imageNamed:@"bugOne4"],nil];
                     self.bugOneButton.imageView.animationDuration = 0.3;
                     [self.bugOneButton.imageView startAnimating];


                     self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOneButton.center.y - 50);
                     self.bugOnePositionY = self.bugOnePositionY - 50;


                 }completion:^(BOOL finished) {
                     [self.bugOneButton.imageView stopAnimating];
                 }];

self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY);  
}

【问题讨论】:

  • 您在完成块之后再次将中心设置为初始位置..
  • 移除这个 self.bugOnePositionY = self.bugOnePositionY - 50;
  • 谢谢,上面的Y位置改了,为什么没改?

标签: ios objective-c animation uiviewanimation


【解决方案1】:

删除代码中的以下行

self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY); 

它会修复..

【讨论】:

    【解决方案2】:

    @Genie Wanted 说的对,去掉代码

    self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOnePositionY)
    

    这将重置按钮位置!

    【讨论】:

    • 我认为你使用了第三方库,那么动画时间比你的动画时间长,所以当它完成时,会重置它。或者方法 stopAnimating 导致
    【解决方案3】:

    去掉这两行

    - (IBAction)bugOneTapped:(id)sender
    {
       [UIView animateWithDuration:1.0
                     animations:^{
                         self.bugOneButton.imageView.animationImages =
                         [NSArray arrayWithObjects:[UIImage imageNamed:@"bugOne3"],[UIImage imageNamed:@"bugOne2"],[UIImage imageNamed:@"bugOne1"],[UIImage imageNamed:@"bugOne2"],[UIImage imageNamed:@"bugOne3"],[UIImage imageNamed:@"bugOne4"],[UIImage imageNamed:@"bugOne5"],[UIImage imageNamed:@"bugOne4"],nil];
                         self.bugOneButton.imageView.animationDuration = 0.3;
                         [self.bugOneButton.imageView startAnimating];
    
                         self.bugOneButton.center = CGPointMake(self.bugOnePositionX, self.bugOneButton.center.y - 50);
    
                     }completion:^(BOOL finished) {
                         [self.bugOneButton.imageView stopAnimating];
                     }];
    
    
    }
    

    【讨论】:

    • 当你使用 AnimationBlocks 时,也许这里不需要 startAnimating 和 stopAnimating
    • 我认为他正在使用任何第三方库制作动画@GenieWanted
    猜你喜欢
    • 2013-03-03
    • 2013-03-22
    • 1970-01-01
    • 2011-08-28
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多