Ios 将UIImageView图像更改为翻转动画的一半
如何在翻转动画的中途更改UIImageView的图像。 我的代码似乎确实翻转了UIImageView并成功地更改了图像,但它正在瞬间更改。我想要实现的是,仅在180度翻转达到90度翻转时才更改图像。这是我的密码: 在视图上创建UIImageView:Ios 将UIImageView图像更改为翻转动画的一半,ios,objective-c,animation,uiview,cgaffinetransform,Ios,Objective C,Animation,Uiview,Cgaffinetransform,如何在翻转动画的中途更改UIImageView的图像。 我的代码似乎确实翻转了UIImageView并成功地更改了图像,但它正在瞬间更改。我想要实现的是,仅在180度翻转达到90度翻转时才更改图像。这是我的密码: 在视图上创建UIImageView: UIImage *image = [UIImage imageNamed:@"fb1.jpg"]; imageView = [[UIImageView alloc] initWithImage:image]; vw = [[UIVi
UIImage *image = [UIImage imageNamed:@"fb1.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
vw = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
vw.layer.cornerRadius = vw.frame.size.width / 2;
imageView.frame = CGRectMake(20, 20, 80, 80);
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [[UIColor blackColor] CGColor];
imageView.layer.borderWidth = 1.0;
[self.view addSubview: imageView];
//[self.view addSubview:vw];
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taptap:)];
[imageView addGestureRecognizer:tap];
点击时设置UIImageView的动画:
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
imageView.transform = CGAffineTransformMakeScale(-1, 1);
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
imageView.image = [UIImage imageNamed:@"fb2.jpg"];
} completion:^(BOOL finished) {
}];
}
completion:^(BOOL finished) {
}];
另外,我在特定UIImageView上以点击手势制作此动画。您可以轻松使用
UIView
类方法+(void)transitionWithView:duration:options:animations:completion:
在动画中使用如下代码:
[UIView transitionWithView:imageView
duration:0.4
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
// Set the new image
// Since its done in animation block, the change will be animated
imageView.image = newImage;
} completion:^(BOOL finished) {
// Do whatever when the animation is finished
}];
您可以通过将UIViewAnimationOptionTransitionFlipFromRight
替换为以下任一选项来设置翻转方向:
UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
尝试两步动画,如果不喜欢,可以查找文档中的连接选项
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn
animations:^(void) {
//do your half rotation here
imageView.transform = CGAffineTransformMakeScale(0, 1);
}
completion:^(BOOL finished) {
if(finished){
imageView.image = [UIImage imageNamed:@"fb2.jpg"];
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
animations:^(void) {
//do your second half rotation here
imageView.transform = CGAffineTransformMakeScale(-1, 1);
} completion:^(BOOL finished) {
}];
}
}];
斯威夫特
UIView.transition(with: imageView,
duration: 0.4,
options: UIView.AnimationOptions.transitionFlipFromLeft,
animations: {
imageView.image = newImage
}, completion: nil)
我试图遵循的答案是,当硬币翻转开始时,我的UIImageView会变成正方形。谢谢你,但我需要的动画是一个像硬币翻转一样的动画,我只想在硬币翻转到一半时改变图像。我不明白你想做什么。您想要180°翻转动画(从纸张的一侧到另一侧)还是完整的360°翻转旋转动画?我的代码制作180°翻转动画:制作90°(半翻转),使图像的视觉宽度=0,更改图像并将宽度返回到原始宽度),这就是我试图实现的。在半翻转之后,我希望图像立即被更改。好的,你的代码似乎正在做我想做的事情。但是我忘了提到一个重要的细节,那就是我正在翻转的UIImageView是四舍五入的。它的角半径是其高度的一半,所以看起来像一个圆。翻转时,它会变成一个正方形,然后又变成一个圆形。有什么建议吗?没有理由这么做。我自己刚试过,设置了角半径,并且在翻转动画过程中正确保持了半径。也许你在别的地方有问题