今天继续练习动画方面,了解一下CATransition 方面用法。不知道大家有没有发现学ios的时候,有一个很怪现象,在学一段时间很容易忘记了自己写过的东西,过一段时间后又会把前几个星期写的程序都忘记了。这种有点像你吞了一个物体,它在反抗你。
这个练习就是试每一个CATransition的效果是怎样的,顺便看一下UIPickerView 是怎么一回事。他的代理方式类似 TableView,这个代理模式用到场景太多了,有时候多少会觉得罗嗦一点。写熟悉了也没所谓了。
切换后,这个程序就可以看到每一个效果情况了。唯一个kCATransitionFade 这个情况不知道发生什么事,没看到什么效果存在。
UIKit 组件众多,每一个尝试一下都要花一点时间。虽然社区和文档都很成熟,很多有价值的blog 都有相应的解答。这些资源相当宝贵。 有时候还是需要尝试一下断网开发才行,至少这样才可以训练一下找文档的能力。现在每次百度找答案有时候有时候觉得自己开始会变笨了。尽管找问题能力很棒,解决问题就弱化了。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
//创建一个选择器
@property (nonatomic,strong) UIPickerView *pickerView;
@end
#import "ViewController.h"
@interface ViewController ()
{
NSArray *pickerArray;
NSArray *typeArray;
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pickerArray = @[@"kCATransitionFade",
@"pageCurl",
@"pageUnCurl",
@"rippleEffect",
@"suckEffect",
@"cube",
@"oglFlip",
@"MoveIn+kCATransitionFromRight",
@"MoveIn+kCATransitionFromLeft",
@"MoveIn+kCATransitionFromTop",
@"MoveIn+kCATransitionFromBottom",
@"Push+kCATransitionFromRight",
@"Push+kCATransitionFromLeft",
@"Push+kCATransitionFromTop",
@"Push+kCATransitionFromBottom",
@"Reveal+kCATransitionFromRight",
@"Reveal+kCATransitionFromLeft",
@"Reveal+kCATransitionFromTop",
@"Reveal+kCATransitionFromBottom"
];
// pageCurl 向上翻一页
// pageUnCurl 向下翻一页
// rippleEffect 滴水效果
// suckEffect 收缩效果,如一块布被抽走
// cube 立方体效果
// oglFlip 上下翻转效果
typeArray = @[ @[kCATransitionFade],
@[@"pageCurl"],
@[@"pageUnCurl"],
@[@"rippleEffect"],
@[@"suckEffect"],
@[@"cube"],
@[@"oglFlip"],
@[kCATransitionMoveIn,kCATransitionFromRight],
@[kCATransitionMoveIn,kCATransitionFromLeft],
@[kCATransitionMoveIn,kCATransitionFromTop],
@[kCATransitionMoveIn,kCATransitionFromBottom],
@[kCATransitionPush,kCATransitionFromRight],
@[kCATransitionPush,kCATransitionFromLeft],
@[kCATransitionPush,kCATransitionFromTop],
@[kCATransitionPush,kCATransitionFromBottom],
@[kCATransitionReveal,kCATransitionFromRight],
@[kCATransitionReveal,kCATransitionFromLeft],
@[kCATransitionReveal,kCATransitionFromTop],
@[kCATransitionReveal,kCATransitionFromBottom]
];
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bbg.jpg"]];
[self.view addSubview:imageView];
//选择器
self.pickerView =[[UIPickerView alloc]init];
[self.view addSubview:self.pickerView];
self.pickerView.frame =(CGRect){0,self.view.frame.size.height-180+20,320,180};
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.backgroundColor = [UIColor orangeColor];
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerArray count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSArray *typeTempArray =typeArray[row];
CATransition *transaction =[CATransition animation] ;
if ([typeTempArray count] ==1)
{
transaction.type = typeTempArray[0] ;
transaction.duration = 1;
}
else
{
transaction.type = typeTempArray[0];
transaction.subtype = typeTempArray[1];
transaction.duration = 1;
}
[imageView.layer addAnimation:transaction forKey:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end