通过左右手势实现简单的照片翻页功能
首先在加载UI的时候添加左右手势
//添加向右滑动的手势
UISwipeGestureRecognizer *Recognizer;
Recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hanleSwipAction:)];
[Recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
//添加向左滑动的手势
[self.view addGestureRecognizer:Recognizer];
Recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hanleSwipAction:)];
[Recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:Recognizer];
//滑动手势执行的动作(这里的数组模型是通过collectionView的item点击事件传入的)
-(void)hanleSwipAction:(UISwipeGestureRecognizer *)recognizer{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右滑动");
if (self.curruntCount > 0) {
self.curruntCount = self.curruntCount - 1;
ExpertDetailPhoto *model = self.icons[self.curruntCount];
self.pageNumLabel.text = [NSString stringWithFormat:@"%d/%lu",self.curruntCount + 1,(unsigned long)self.icons.count];
[self.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"ImageDefault"]];
NSString *subtypeString;
subtypeString = kCATransitionFromLeft;
[self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view];
}else{
self.curruntCount = 0;
}
}else if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左滑动");
if (self.curruntCount < (int)(self.icons.count -1)) {
self.curruntCount = self.curruntCount + 1;
ExpertDetailPhoto *model = self.icons[self.curruntCount];
self.pageNumLabel.text = [NSString stringWithFormat:@"%d/%lu",self.curruntCount + 1,(unsigned long)self.icons.count];
[self.photoImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"ImageDefault"]];
NSString *subtypeString;
subtypeString = kCATransitionFromRight;
[self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view];
}else{
self.curruntCount =(int)(self.icons.count - 1);
}
}
}
//执行动画的方法
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view {
//实例化动画对象
CATransition *animation = [CATransition animation];
//动画执行时间
animation.duration = 0.5f;
//动画类型
animation.type = type;
if (subtype != nil) {
animation.subtype = subtype;
}
//动画执行过程效果(例:如淡进淡出)
animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
//执行动画操作
[view.layer addAnimation:animation forKey:@"animation"];
//view的layer层动画.
}