UIScrollView做循环图片

#import "ViewController.h"
#define IMAGE_COUNT 6
@interface ViewController ()
{
    UIImageView *myImageView;
    int currentIndex;

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    myImageView=[[UIImageView alloc]init];
    myImageView.frame=[UIScreen mainScreen].applicationFrame;
    myImageView.contentMode=UIViewContentModeScaleAspectFit;
    myImageView.image=[UIImage imageNamed:@"1"];
    [self.view addSubview:myImageView];
    
    UISwipeGestureRecognizer *leftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
    leftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftGesture];
    
    UISwipeGestureRecognizer *rightGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
    leftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:rightGesture];
    
}

-(void)leftSwipe:(UISwipeGestureRecognizer *)gesture
{
    [self transitionAnimation:YES];

}
-(void)rightSwipe:(UISwipeGestureRecognizer *)gesture
{
    [self transitionAnimation:NO];
    
}
-(void)transitionAnimation:(BOOL)isNext
{
    CATransition *transition=[[CATransition alloc]init];
    transition.type=@"cube";
    if (isNext) {
        transition.subtype=kCATransitionFromRight;
    }
    else
    {
        transition.subtype=kCATransitionFromLeft;
    }
    transition.duration=2.0f;
    myImageView.image=[self getImage:isNext];
    [myImageView.layer addAnimation:transition forKey:@"KCTransitionAnimation"];
}
-(UIImage *)getImage:(BOOL)isNext
{
   if(isNext)
   {
       currentIndex=(currentIndex+1)%IMAGE_COUNT;
   }
   else
   {
       currentIndex=(currentIndex-1+IMAGE_COUNT)%IMAGE_COUNT;
   }
    NSString *imageName=[NSString stringWithFormat:@"%d",currentIndex];
    return [UIImage imageNamed:imageName];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end

posted @ 2015-03-12 22:22  曹县三胖暴打大猩猩  阅读(128)  评论(0编辑  收藏  举报