- 1 . 閑來(lái)無(wú)聊,研究了下CATransition動(dòng)畫(huà)贩虾,發(fā)現(xiàn)原來(lái)幅狮,還是很實(shí)用,想了下決定試著看能否封裝下,最后在使用幾種方法后,最后使用類(lèi)擴(kuò)展(類(lèi)別)的方法:
-2.在
CATransition
這個(gè)實(shí)體中灼舍,蘋(píng)果有提供我們多個(gè)動(dòng)畫(huà),CATransition的type屬性
1.#define定義的常量
kCATransitionFade 交叉淡化過(guò)渡
kCATransitionMoveIn 新視圖移到舊視圖上面
kCATransitionPush 新視圖把舊視圖推出去
kCATransitionReveal 將舊視圖移開(kāi),顯示下面的新視圖
2.用字符串表示
pageCurl 向上翻一頁(yè)
pageUnCurl 向下翻一頁(yè)
rippleEffect 滴水效果
suckEffect 收縮效果涨薪,如一塊布被抽走
cube 立方體效果
oglFlip 上下翻轉(zhuǎn)效果
因此骑素,我在類(lèi)的擴(kuò)展中定義以下幾種枚舉類(lèi)型:
#import <UIKit/UIKit.h>
/**
* SX動(dòng)畫(huà)擴(kuò)展:
*/
@interface UIView (SXAnimation)
/**
* 動(dòng)畫(huà)類(lèi)型
*/
typedef enum{
SXpageCurl, // 向上翻一頁(yè)
SXpageUnCurl, //向下翻一頁(yè)
SXrippleEffect, //波紋
SXsuckEffect, //吸收
SXcube, //立方體
SXoglFlip, //翻轉(zhuǎn)
SXcameraIrisHollowOpen, //鏡頭開(kāi)
SXcameraIrisHollowClose, //鏡頭關(guān)
SXfade, //翻頁(yè)
SXmovein, //彈出
SXpush //推出
}AnimationType;
/**
* 動(dòng)畫(huà)方向
*/
typedef enum{
SXleft, //左
SXright, //右
SXtop, //頂部
SXbottom, //底部
SXmiddle
}Direction;
-3. 最終再擴(kuò)展一個(gè)類(lèi)別方法,用于設(shè)置我們想要的某些動(dòng)畫(huà)刚夺。
/**
* 動(dòng)畫(huà)設(shè)置
*
* @param animation 動(dòng)畫(huà)
* @param durationTime 動(dòng)畫(huà)時(shí)間
* @param subtype 過(guò)渡方向
*/
- (void)setAnimationWithType:(AnimationType)animation
duration:(float)durationTime
directionSubtype:(Direction)subtype;
-4 .實(shí)現(xiàn)部分献丑,還是很簡(jiǎn)單的
- (void)setAnimationWithType:(AnimationType)animation
duration:(float)durationTime
directionSubtype:(Direction)subtype
{
//CATransition實(shí)體
CATransition* ani=[CATransition animation];
//動(dòng)畫(huà)時(shí)間:
ani.duration = durationTime;
//選擇動(dòng)畫(huà)過(guò)渡方向:
switch (subtype) {
case SXleft:
ani.subtype = kCATransitionFromLeft;
break;
case SXright:
ani.subtype = kCATransitionFromRight;
break;
case SXtop:
ani.subtype = kCATransitionFromTop;
break;
case SXbottom:
ani.subtype = kCATransitionFromBottom;
break;
case SXmiddle:
ani.subtype = kCATruncationMiddle;
break;
default:
break;
}
//選擇動(dòng)畫(huà)效果:
switch (animation)
{
case SXpageCurl:
{
ani.type = @"pageCurl";
}
break;
case SXpageUnCurl:
{
ani.type = @"pageUnCurl";
}
break;
case SXrippleEffect:
{
ani.type = @"rippleEffect";
}
break;
case SXsuckEffect:
{
ani.type = @"suckEffect";
}
break;
case SXcube:
{
ani.type = @"cube";
}
break;
case SXcameraIrisHollowOpen:
{
ani.type = @"cameraIrisHollowOpen";
}
break;
case SXoglFlip:
{
ani.type = @"oglFlip";
}
break;
case SXcameraIrisHollowClose:
{
ani.type = @"cameraIrisHollowClose";
}
break;
case SXmovein:
ani.type = kCATransitionMoveIn;
break;
case SXpush:
ani.type = kCATransitionPush;
break;
case SXfade:
ani.type = kCATransitionFade;
break;
default:
break;
}
//動(dòng)畫(huà)加到圖層上
[self.layer addAnimation:ani forKey:nil];
}
- 5.最后是,該如何使用侠姑? ---我在這里用了一個(gè)滾動(dòng)視圖做的示例:
@interface ViewController ()<UIScrollViewDelegate>
{
//動(dòng)畫(huà)類(lèi)型
AnimationType Type;
//輪播圖
UIScrollView * scroll ;
}
- (void)viewDidLoad {
[super viewDidLoad];
Type = SXrippleEffect;
scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, ScreenWidth, 200)];
scroll.contentSize = CGSizeMake(ScreenWidth*8, 200);
scroll.pagingEnabled = YES;
scroll.delegate = self;
for (NSInteger i = 0; i<8; i++)
{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, 200)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%02ld.jpg",i+1]];
[scroll addSubview:imageView];
}
[self.view addSubview:scroll];
}
6.在滾動(dòng)視圖中代理方法中設(shè)置想要的動(dòng)畫(huà)效果:
//將要拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//調(diào)用設(shè)置動(dòng)畫(huà)
[scroll setAnimationWithType:Type duration:1.0 directionSubtype:SXright];
}
- 7.設(shè)置一個(gè)按鈕创橄,彈出選擇框,選擇想要的效果莽红,設(shè)置
Type = SXpageUnCurl等效果;