- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (dirString)
{
CATransition *animation = [self getAnimation:dirString];
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[[self superview] layer] addAnimation:animation forKey:kAnimationKey];
}
}
1.两张图片手指触摸 左右上下滑动 实现方法
- (CATransition *) getAnimation:(NSString *) direction
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:@"oglFlip"];
[animation setType:kCATransitionPush]; //在下面 有这种动画类型的详细解释
[animation setSubtype:direction];
[animation setDuration:1.0f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
return animation;
}
#define HORIZ_SWIPE_DRAG_MIN 12
#define VERT_SWIPE_DRAG_MAX 4
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
startTouchPosition = [touch locationInView:self]; dirString = NULL;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (dirString)
{
CATransition *animation = [self getAnimation:dirString];
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[[self superview] layer] addAnimation:animation forKey:kAnimationKey];
}
}
实现iphone的动画效果可以分为两种方法一种是UIView层面的一种是是
使用CATransition进行更低层次的控制
第一种是UIView,UIView方式可能在低层也是使用CATransition进行
了封装,它只能用于一些简单的、常用的效果展现
CGContextRef context = UIGraphicsGetCurrentContext() ;
[UIView beginAnimations:nil context:context]; //开始动画
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:[self superview] cache:YES]; //动画的方向
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //动画的形式
[UIView setAnimationDuration:1.0]; //动画的速度以秒为单位
[[self superview]exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//转换的固定格式
[UIView commitAnimations]; //也为固定格式
第二种是用CATransition这个方法
这里使用setType和setSubtype组合使用这个比较保险因为他们的参数在官方API中定义
setType可以有四种返回类型
KCATransitionFade 淡出
KCATransitionMoveIn 从指定的方向用新的图来覆盖旧得图
KCATransitionPush 推出 像观看幻灯片那样从指定的方向例如左
新的图片从左边出来旧的图片向右边去
KCATransitionReveal 从底部显示出来与 MoveIn的效果刚好相反
setSubtype 也有四种返回类型
KCATransitionFromRight KCATransitionFromLeft (默认值)
KCATransitionFromTop KCATransitionFromBottom
还有一种设置动画类型的方法 不用setSubtype 只用setType
[animation setType:@"suckEffect"];
这里的suckEffect就是效果名称,可以用的效果主要有:
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect收缩效果,如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果