iOS-动画效果(图片左右滑动 添加动画效果)

来源:互联网 发布:not close json text 编辑:程序博客网 时间:2024/05/17 07:23
图片左右滑动  添加动画效果

#import "ViewController.h"

#import "NextViewController.h"

typedef enum Direction{

    Right = 0,

    Left,

}Direction;


@interface ViewController ()

{

    NSArray *imageList;

    UIImageView *showImage;

    int index;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    imageList = @[@"tianhe1.jpg",@"tianhe2.jpg",@"tianhe3.jpg"];

    showImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    showImage.image = [UIImage imageNamed:imageList[0]];

    showImage.userInteractionEnabled = YES;

    [self.view addSubview:showImage];

    

    UISwipeGestureRecognizer *rightSWip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(right)];

    rightSWip.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:rightSWip];

    

    UISwipeGestureRecognizer *leftSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(left)];

    leftSwip.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:leftSwip];

    

    UILongPressGestureRecognizer *next = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(next:)];

    [self.view addGestureRecognizer:next];

    

}


- (void)left

{

    [self changeImageWithDirection:Left];

}


- (void)right

{

    [self changeImageWithDirection:Right];

}


- (void)changeImageWithDirection:(Direction)direction

{

    

    

    

    index = direction == Right ? [self countRelease]:[self countAdd];

    

    CATransition *transition = [CATransition animation];

    transition.type = direction == Right ? kCATransitionFade:@"rippleEffect";

    transition.subtype = direction == Right ? kCATransitionFromRight:kCATransitionFromLeft;

    transition.duration = 1.5;

    

    [showImage.layer addAnimation:transition forKey:imageList[index]];

    

    showImage.image = [UIImage imageNamed:imageList[index]];

}


#pragma mark -------  向左滑动图片自加++   ------

//需要通过方向判断是自加还是自减  把计算号的值 赋值给  全局变量index

- (int)countAdd

{

    index ++;

    //如果超出了 图片数组的元素个数 index等于0(修复成0) 如果没有超出 返回自加之后的值

    return index >= imageList.count ? 0:index;

}


#pragma mark -------  向右滑动图片自加++   ------

- (int)countRelease

{

    index --;

    return index < 0 ? (int)imageList.count-1:index;

}



0 0