// // ViewController.m // 图片浏览器 // // Created by Mac on 16/1/11. // Copyright © 2016年 Mac. All rights reserved. // #import "ViewController.h" @interface ViewController () - (IBAction)tap:(UITapGestureRecognizer *)tap; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (nonatomic, strong) NSMutableArray *imgNames; @property (nonatomic, assign) NSInteger currentImgIndex; @end @implementation ViewController //- (NSInteger)currentImgIndex //{ // if (!_currentImgIndex) { // _currentImgIndex = 1; // } // return _currentImgIndex; //} - (NSMutableArray *)imgNames { if (!_imgNames) { NSMutableArray *imgNames =[NSMutableArray array]; for (NSInteger i = 1; i < 10; i ++) { NSString *img = [NSString stringWithFormat:@"%ld",i]; [imgNames addObject:img]; } _imgNames = imgNames; } return _imgNames; } - (void)viewDidLoad { [super viewDidLoad]; // NSLog(@"%@",self.imgNames); // Do any additional setup after loading the view, typically from a nib. // self.imageView self.currentImgIndex = 1; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)tap:(UITapGestureRecognizer *)tap{ CGPoint location = [tap locationInView:tap.view]; if (location.x <= self.view.bounds.size.width /2) { // NSLog(@"在左边"); [self preImg]; }else { // NSLog(@"在右边"); [self nextImg]; } } - (void)preImg { if (self.currentImgIndex == 1) { } else{ self.currentImgIndex -- ; NSString *preImgName = [NSString stringWithFormat:@"%ld",(self.currentImgIndex )]; NSLog(@"%@",preImgName); self.imageView.image = [UIImage imageNamed:preImgName]; CATransition *animation = [CATransition animation]; animation.type = @"pageUnCurl"; animation.subtype = @"fromRight"; animation.duration = 0.45; [self.imageView.layer addAnimation:animation forKey:nil]; } } - (void)nextImg { if (self.currentImgIndex == self.imgNames.count) { NSLog(@"最后"); }else{ self.currentImgIndex++; NSString *nextImgName = [NSString stringWithFormat:@"%ld",self.currentImgIndex]; NSLog(@"%@",nextImgName); self.imageView.image =[UIImage imageNamed:nextImgName]; CATransition *animation = [CATransition animation]; animation.type = @"pageCurl"; animation.subtype = @"fromRight"; animation.duration = 0.45; [self.imageView.layer addAnimation:animation forKey:nil]; } } @end
原文:http://www.cnblogs.com/BJTUzhengli/p/5122143.html