简体   繁体   English

UIView.animateWithDuration:动画始终在原始位置停止

[英]UIView.animateWithDuration: animation always stops at original position

I added an ImageView in the middle of my view (no constraints set). 我在视图中间添加了一个ImageView (未设置约束)。

With this code I want to move this ImageView from its original position to a new one. 通过此代码,我想将此ImageView从其原始位置移动到新位置。

Unfortunately, it moves from the new to its original position - why? 不幸的是,它从新的位置移到了原来的位置-为什么?

let opts = UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(1, delay: 0, options: opts, animations: {
     self.ivSun.center.x += 100
}, completion: nil) 

Add this after completion of your animation. 动画完成后添加。

self.ivSun.layer.position = self.ivSun.layer.presentationLayer().position

I have code that implemented in my game. 我有在游戏中实现的代码。

        CATransaction.begin()
        CATransaction.setCompletionBlock { () -> Void in
                    self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position
            }
        }
        var animation = CABasicAnimation(keyPath: "position")
        animation.duration = ballMoveTime
        animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
        animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        viewBall.layer.addAnimation(animation, forKey: "transform")
        CATransaction.commit()

Change position value as you need. 根据需要更改头寸值。

Try this one. 试试这个。

func animateImage()
{
    UIView.animateWithDuration(3.0, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.imageView.center = CGPointMake(self.imageView.center.x+10, self.imageView.center.y+10)
    }, completion: nil)
}

Without knowing more about the context and related code it's difficult to give an answer on "why" it's snapping back to its original position, but as an alternative you could set the completion handler with whatever you'd like as the final position/coordinate. 在不了解上下文和相关代码的情况下,很难给出“为什么”迅速回复到其原始位置的答案,但是作为替代方案,您可以根据需要将完成处理程序设置为最终位置/坐标。

Generally I use constraints on my widgets/views, and I animate their values (versus animating the widgets' frame, for example) ... with excellent results. 通常,我在小部件/视图上使用约束,并对它们的值进行动画处理(例如,对小部件的框架进行动画处理)...具有出色的效果。

This is due to autolayout. 这是由于自动布局。 Constraints need to be changed and updated in the the animation as seen below: 需要在动画中更改和更新约束,如下所示:

- (IBAction)moveAction:(id)sender {
    self.spaceConstraint.constant += 220;

    [UIView animateWithDuration:0.5 animations:^{
         [self.imageView layoutIfNeeded];
    }];
}
var animation = CABasicAnimation(keyPath: "position")
animation.duration = ballMoveTime
animation.fromValue = NSValue(CGPoint: viewBall.layer.presentationLayer().position)
animation.toValue = NSValue(CGPoint: CGPointMake(self.borderRight, self.borderMiddle))
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeForwards
viewBall.layer.addAnimation(animation, forKey: "transform")
CATransaction.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM