[转载]Flip an image in UIImageView using UIView transitionWithView

本文介绍如何使用UIView的transitionWithView方法实现UIImageView中图片的翻转动画效果,通过简单的代码示例展示从一张图片平滑过渡到另一张图片的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

View animations on the iPhone are wonderful. Used properly they will delight your users and help your application stand out. The iOS provides a suite of methods for animating your interface, including the excellent  UIView class method + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations, which takes a block of animations that let you, for example, smoothly resize or move a view or adjust it’s alpha value to fade it in and out.

But what if you want to do a more complex transition? You might be tempted to dip into Core Animation and transformation matrixes. You have complete control over the animation, the timing, callbacks and so on. For a quick flip or other common transformation, though, UIView remains your best friend.

UIView offers two methods for complex animated transitions:

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView 
	duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options 
	completion:(void (^)(BOOL finished))completion
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration 
	options:(UIViewAnimationOptions)options animations:(void (^)(void))animations 
	completion:(void (^)(BOOL finished))completion

These aren’t the most straightforward methods, as the confusion as StackOverflow indicates, and the documentation doesn’t clearly explain what the fromViewtoView and withView should be or even the animations themselves. Yet, for a particular kind of transition, things couldn’t be easier.

In our case we want to transition from one image to another using UIImageView. Specifically we want to flip from one image to the other, with the second image as the backside of the first. Like turning a business card over to view the reverse side.

The first point to be clear on is the views, or view, involved. You might think that a transition will involve two UIImageViews, each displaying a single image, and that we would use the transitionFromView:toView: method. While it might be possible to do it this way, we have it much simpler.

It seems that UIImageView has some transition deliciousness baked right into it.  We don’t actually need two separate image views each holding its own image. UIImageView can perform the transition itself. We only need the single UIImageView and the two images we want. Which means we’ll be using the transitionWithView:method instead.

Make sure you have a UIImageView on the screen and it is already displaying an image. Flip from the first image to the next with a single call to transitionWithView:

UIImageView *imageView = ...;
UIImage *secondImage = ...;

[UIView transitionWithView:imageView duration:0.5 
	options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
	imageView.image = secondImage;
} completion:nil];

That’s it. With our view in place, the second image loaded, and our animation specified, the UIImageView transition machinery takes care of everything. We end up with a lovely transition from the front side to the back side of a single image view with nothing more than a familiar call to the UIImageView’s image setter.

Animations are awesome. From the start they helped make the iPhone a fun, amazing device. While complex animations can be confusing, the iOS helps, and in many cases you don’t need to look any further than UIKit.

 

 

from:http://phildow.net/2012/05/31/flip-an-image-in-uiimageview-using-uiview-transitionwithview/

转载于:https://www.cnblogs.com/yinghuochong/p/3399285.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值