在iPhone中动画过度非常简单.
首先获取当前的图形上下文:
CGContextRef context = UIGraphicsGetCurrentContext();
接着设置一些动画属性用于开始动画:
[UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0];
然后设置想要过度的对象的最终状态.
[big setFrame:SMALLRECT]; [big setAlpha:0.5]; [little setFrame:BIGRECT]; [little setAlpha:1.0];
最后提交动画,这样一个动画就会自动生成了
[UIView commitAnimations];
其中,setAnimationCurve是设置动画的方式,他有下面集中方式:
// Start Animation Block //CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRef context = nil; [UIView beginAnimations:nil context:context]; [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; // Animations [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; // Commit Animation Block [UIView commitAnimations];
typedef enum { UIViewAnimationTransitionNone, UIViewAnimationTransitionFlipFromLeft, UIViewAnimationTransitionFlipFromRight, UIViewAnimationTransitionCurlUp, UIViewAnimationTransitionCurlDown, } UIViewAnimationTransition;
除了这种简单的动画方式外,其实还有一种利用QuartzCore来做过度动画.不同的地方在于,这个过度动画作用于层,换句话说,他动画直接做用于整个UIView,而不像UIView的动画,可以作用于UIView局部或本身.当UIView作用与本身时,实际上也就相当于是对层的动画了.
CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setDuration:1.0f]; [animation setTimingFunction:UIViewAnimationCurveEaseInOut]; [animation setType: kCATransitionMoveIn]; [animation setSubtype: kCATransitionFromBottom]; [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; [[[self superview] layer] addAnimation:animation forKey:@"transitionViewAnimation"];
setDuration:和UIView中的动画效果一样,持续时间.
setTimingFunction:是动画的种类,和UIView一样,比如匀速动画,快速开始结束等.
setType:是指定动画的类型,他有:
// Curl the image up or down. This runs only on the iPhone and will not // produce any effect from the simulator CATransition *animation = [CATransition animation]; [animation setDelegate:self]; [animation setDuration:1.0f]; [animation setTimingFunction:UIViewAnimationCurveEaseInOut]; [animation setType:(notCurled ? @"pageCurl" : @"pageUncurl")]; /* mapCurl, mapUncurl pageCurl, pageUncurl suckEffect, spewEffect, cameraIris, cameraIrisHollowOpen, cameraIrisHollowClose genieEffect, unGenieEffect, rippleEffect, twist, tubey, swirl, charminUltra zoomIn, zoomOut oglFlip */ //让他不给删除掉 [animation setRemovedOnCompletion:NO]; [animation setFillMode: @"extended"]; [animation setRemovedOnCompletion: NO]; notCurled = !notCurled; [[[self.view viewWithTag:TOP_LAYER_VIEW] layer] addAnimation:animation forKey:@"pageFlipAnimation"];