gpt4 book ai didi

ios - 以编程方式为具有约束的 UIImageView(View)从 A 点到 B 点设置动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:30 25 4
gpt4 key购买 nike

我有一个 ImageView ,我已将它排列在屏幕的中央右侧。在有约束的情况下单独添加 ImageView 时一切都很好。我需要将 ImageView 从原始位置(A 点)动画化到屏幕的底部(B 点);问题是当我尝试从 A 点到 B 点设置动画时, ImageView 从屏幕的左上角开始,就像它是在没有约束的情况下简单添加的(而不是我希望它在中心)。

这是代码和一些注释:

#define DEGREES_TO_RADIANS(x) (M_PI * x / 180.0) //defined above
- (void)viewDidLoad
{
UIImage *cardFaceDownImage = [UIImage imageNamed:@"b-top@2x.png"];
UIImageView *dealCardDown = [[UIImageView alloc] initWithImage:cardFaceDownImage];
dealCardDown.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:1];

NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:130];

[self.view addConstraint:dealCardConstraintY];
[self.view addConstraint:dealCardConstraintX];
[self.view addSubview:dealCardDown];

//IF I comment out the below code the UIImageView lines up where I want it (Point A)
//IF I don't coment it out, the imageview starts in the upper left hand corner of the screen
//where it normally would as if just adding an image view programatically without
//constraints would, but it does end up where I want it to (Point B)

[self.view removeConstraint:dealCardConstraintX];
[self.view removeConstraint:dealCardConstraintY];

dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:1];

dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:130];

[self.view addConstraint:dealCardConstraintY];
[self.view addConstraint:dealCardConstraintX];

[self.view setNeedsUpdateConstraints];

[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationCurveEaseIn animations:^(void)
{
dealCardDown.transform = CGAffineTransformRotate(dealCardDown.transform, DEGREES_TO_RADIANS(90));

[self.view layoutIfNeeded];

}
completion:^(BOOL finished)
{
}
];
}

..

最佳答案

您需要将约束的变化移动到动画 block 中,以便正确地进行动画处理。这段代码对我很有效。

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

UIView *dealCardDown = [[UIView alloc] initWithFrame:(CGRect){0,0,50,50}];
dealCardDown.backgroundColor = [UIColor redColor];
dealCardDown.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *sizeConstraint = [NSLayoutConstraint constraintWithItem: dealCardDown attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 50];
[dealCardDown addConstraint:sizeConstraint];
sizeConstraint = [NSLayoutConstraint constraintWithItem: dealCardDown attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 50];
[dealCardDown addConstraint:sizeConstraint];
[self.view addSubview:dealCardDown];

NSLayoutConstraint *dealCardConstraintX = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0.f];
NSLayoutConstraint *dealCardConstraintY = [NSLayoutConstraint constraintWithItem:dealCardDown attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.f constant:-self.view.center.y+dealCardDown.frame.size.height/2];


[self.view addConstraint:dealCardConstraintX];
[self.view addConstraint:dealCardConstraintY];
[self.view layoutIfNeeded];

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void)
{
dealCardDown.transform = CGAffineTransformRotate(dealCardDown.transform, 90*M_PI/180);
dealCardConstraintY.constant = 0;
[self.view layoutIfNeeded];

}
completion:^(BOOL finished)
{
}
];
}

关于ios - 以编程方式为具有约束的 UIImageView(View)从 A 点到 B 点设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14913949/

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