gpt4 book ai didi

ios - 将 UIImageView 从左侧移动到中心

转载 作者:行者123 更新时间:2023-11-28 18:16:02 27 4
gpt4 key购买 nike

我想将 ImageView 从左侧移动到 View 的中心。

以下是我的代码:

CGPoint origin = self.headerImage.center;
CGPoint target = CGPointMake(0, self.headerImage.center.x);

CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.x"];
bounce.fromValue = [NSNumber numberWithInt:origin.x];
bounce.toValue = [NSNumber numberWithInt:target.x];
[self.headerImage.layer addAnimation:bounce forKey:@"position"];

它必须从左侧开始移动,并在加载 View Controller 时停在中心。但它不能正常工作。我该如何解决这个问题?

最佳答案

  1. target 需要表示 View 的中心:CGPointMake(self.view.center.x, self.view.center.y)
  2. 更改fillMode removedOnCompletion autoreverses repeatCount 以避免帧更改(动画的最后)

最终代码:

CGPoint origin = self.headerImage.center;
CGPoint target = CGPointMake(self.view.center.x, self.view.center.y);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.x"];
bounce.duration = 1;
bounce.fromValue = [NSNumber numberWithInt:origin.x];
bounce.toValue = [NSNumber numberWithInt:target.x];
bounce.fillMode = kCAFillModeForwards;
bounce.removedOnCompletion = NO;
bounce.autoreverses = NO;
bounce.repeatCount = 0;
[self.headerImage.layer addAnimation:bounce forKey:@"position"];
CGAffineTransform transform = CGAffineTransformMakeScale(1.3, 1.3);
self.headerImage.transform = transform;

关于ios - 将 UIImageView 从左侧移动到中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com