一个拖拽图片渐变切换的启动页

#import "GuideViewController.h"
#import "SCTabbarController.h"
@interface GuideViewController ()
@property (nonatomic,  strong) UIImageView *firstImgV;
@property (nonatomic,  strong) UIImageView *secondmgV;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic,  strong) UIButton  *overButton;
@property (nonatomic,  strong) NSArray *imgArr;

@property (nonatomic,  assign) BOOL firstImgVDown;
@property (nonatomic,  assign) BOOL go;
@property (nonatomic,  assign) NSInteger imgIdx;

@property (nonatomic,  assign) CGFloat firstImgVPanBeginAlpha;

@property (nonatomic,  strong) NSString *startTime;
@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.secondmgV];
    [self.view addSubview:self.firstImgV];
    [self.view addSubview:self.pageControl];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(WINDOW_height - 50);
    }];
    
    [self.view addSubview:self.overButton];
    CGFloat bottom = -60;
    
    [self.overButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(bottom);
        make.width.mas_equalTo(141);
        make.height.mas_equalTo(42);
    }];
    
    self.go = YES;
    self.firstImgVDown = YES;
    self.imgIdx = 0;
}
- (void)panGRAct: (UIPanGestureRecognizer *)pan {
    //获取移动的大小
    CGPoint point=[pan translationInView:pan.view];
    if (point.x == 0) {
        return;
    }
    CGFloat fabsTmp = fabs(point.x / (WINDOW_width * 5));
    if (point.x < 0) { // 左滑
        if (self.imgIdx == self.imgArr.count - 1) {
            return;
        }
        self.overButton.alpha = 0.0;
        self.go = YES;
        if (self.firstImgVDown) {
            self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];  // 显示第2个
            self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx + 1]];
            CGFloat tmpFirstImgV = self.firstImgV.alpha - fabsTmp;
            if (tmpFirstImgV < 0.0) {
                self.firstImgV.alpha = 0.0;
            } else {
                self.firstImgV.alpha = tmpFirstImgV;
            }
            if (tmpFirstImgV >= 1) {
                self.firstImgV.alpha = 1;
            }
            CGFloat tmpSecondImgV = self.secondmgV.alpha + fabsTmp;
            if (tmpSecondImgV < 0.0) {
                self.secondmgV.alpha = 0.0;
            } else {
                self.secondmgV.alpha = tmpSecondImgV;
            }
            if (tmpSecondImgV >= 1) {
                self.secondmgV.alpha = 1;
            }
        } else {
            self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];  // 显示第1个
            self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx + 1]];
            CGFloat tmpFirstImgV = self.firstImgV.alpha + fabsTmp;
            if (tmpFirstImgV < 0.0) {
                self.firstImgV.alpha = 0.0;
            } else {
                self.firstImgV.alpha = tmpFirstImgV;
            }
            if (tmpFirstImgV >= 1) {
                self.firstImgV.alpha = 1;
            }
            CGFloat tmpSecondImgV = self.secondmgV.alpha - fabsTmp;
            if (tmpSecondImgV < 0.0) {
                self.secondmgV.alpha = 0.0;
            } else {
                self.secondmgV.alpha = tmpSecondImgV;
            }
            if (tmpSecondImgV >= 1) {
                self.secondmgV.alpha = 1;
            }
        }

    } else { // 右滑
        if (self.imgIdx == 0) {
            return;
        }
        self.overButton.alpha = 0.0;
        self.go = NO;
        if (self.firstImgVDown == NO) {
            
            self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx - 1]];  // 显示第1个
            self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
            CGFloat tmpFirstImgV = self.firstImgV.alpha + fabsTmp;
            if (tmpFirstImgV < 0.0) {
                self.firstImgV.alpha = 0.0;
            } else {
                self.firstImgV.alpha = tmpFirstImgV;
            }
            if (tmpFirstImgV >= 1) {
                self.firstImgV.alpha = 1;
            }
            CGFloat tmpSecondImgV = self.secondmgV.alpha - fabsTmp;
            if (tmpSecondImgV < 0.0) {
                self.secondmgV.alpha = 0.0;
            } else {
                self.secondmgV.alpha = tmpSecondImgV;
            } 
            if (tmpSecondImgV >= 1) {
                self.secondmgV.alpha = 1;
            }
        } else {
            self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx - 1]];  // 显示第2个
            self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
            CGFloat tmpFirstImgV = self.firstImgV.alpha - fabsTmp;
            if (tmpFirstImgV < 0.0) {
                self.firstImgV.alpha = 0.0;
            } else {
                self.firstImgV.alpha = tmpFirstImgV;
            }
            if (tmpFirstImgV >= 1) {
                self.firstImgV.alpha = 1;
            }
            CGFloat tmpSecondImgV = self.secondmgV.alpha + fabsTmp;
            if (tmpSecondImgV < 0.0) {
                self.secondmgV.alpha = 0.0;
            } else {
                self.secondmgV.alpha = tmpSecondImgV;
            }
            if (tmpSecondImgV >= 1) {
                self.secondmgV.alpha = 1;
            }
        } 
    } 
    if(pan.state == UIGestureRecognizerStateEnded) {
        if ((self.imgIdx == 0 && self.go == NO) || (self.imgIdx == self.imgArr.count - 1 && self.go == YES) ) {
            return;
        }
        NSString *endTime = [self getCurrentTimeStamp];
        long long gap = [self getDurationStartTime:self.startTime endTime:endTime];

        if (gap <= 1) {
            if (self.go) {
                self.imgIdx += 1;
            } else {
                self.imgIdx -= 1;
            }
            self.pageControl.currentPage = self.imgIdx;
            if (self.imgIdx % 2 == 1) {
                self.firstImgV.alpha = 0.0;
                self.secondmgV.alpha = 1;
                self.firstImgVDown = NO;
                self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
            } else {
                self.firstImgV.alpha = 1;
                self.secondmgV.alpha = 0.0;
                self.firstImgVDown = YES;
                self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
            }
            if (self.imgIdx == self.imgArr.count - 1) {
                [UIView animateWithDuration:0.2 animations:^{
                    self.overButton.alpha  = 1;
                }];
            } else {
                self.overButton.alpha  = 0;
            }
            return;
        }
        if (self.firstImgV.alpha < 0.5) {
            self.firstImgV.alpha = 0.0;
            self.secondmgV.alpha = 1;
            self.firstImgVDown = NO;
            if (self.firstImgVPanBeginAlpha == self.firstImgV.alpha) { // 前进不到
                
            } else {
                if (self.go) {
                    self.imgIdx += 1;
                }else{
                    self.imgIdx -= 1;
                }
            }
            self.secondmgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
        } else {
            self.secondmgV.alpha = 0.0;
            self.firstImgV.alpha = 1;
            self.firstImgVDown = YES;
            if (self.firstImgVPanBeginAlpha == self.firstImgV.alpha) { // 前进不到
                
            } else {
                if (self.go) {
                    self.imgIdx += 1;
                }else{
                    self.imgIdx -= 1;
                }
            }
            self.firstImgV.image = [UIImage imageNamed:self.imgArr[self.imgIdx]];
        }
        self.pageControl.currentPage = self.imgIdx;
        
        if (self.imgIdx == self.imgArr.count - 1) {
            [UIView animateWithDuration:0.2 animations:^{
                self.overButton.alpha  = 1;
            }];
        } else {
            self.overButton.alpha  = 0;
        } 
    }
    if (pan.state == UIGestureRecognizerStateBegan) {
        if (self.firstImgV.alpha > 0.5) {
            self.firstImgVPanBeginAlpha = 1;
        } else {
            self.firstImgVPanBeginAlpha = 0;
        }
        self.startTime = [self getCurrentTimeStamp];
    }
}

//点击完成按钮
- (void)clickOverButton {
    SCTabbarController *tabBarVc = [[SCTabbarController alloc] init];
    // 切换传口的根控制器 rootViewController
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
}

- (UIImageView *)firstImgV {
    if (_firstImgV == nil) {
        _firstImgV = [[UIImageView alloc] init];
        _firstImgV.image = [UIImage imageNamed:self.imgArr[0]];
        _firstImgV.frame = CGRectMake(0, 0, WINDOW_width, WINDOW_height);
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGRAct:)];
        [_firstImgV setUserInteractionEnabled:YES];
        [_firstImgV addGestureRecognizer:panGR];
        
    }
    return _firstImgV;
}
- (UIImageView *)secondmgV {
    if (_secondmgV == nil) {
        _secondmgV = [[UIImageView alloc] init];
        _secondmgV.image = [UIImage imageNamed:self.imgArr[1]];
        _secondmgV.frame = CGRectMake(0, 0, WINDOW_width, WINDOW_height);
        _secondmgV.alpha = 0.0;
        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGRAct:)];
        [_secondmgV setUserInteractionEnabled:YES];
        [_secondmgV addGestureRecognizer:panGR];
    }
    return _secondmgV;
}
- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl  alloc]init];
        _pageControl.numberOfPages = self.imgArr.count;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = kLightGrayColor;
    }
    return _pageControl;
}
- (UIButton *)overButton {
    if (_overButton == nil) {
        _overButton = [[UIButton alloc]init];
        [_overButton setTitle:@"开启雪鸦租车" forState:UIControlStateNormal];
        [_overButton setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_overButton viewCornerRadius:21 borderColor:kBlackColor];
        [_overButton addTarget:self action:@selector(clickOverButton) forControlEvents:UIControlEventTouchUpInside];
        _overButton.alpha = 0.0;
    }
    return _overButton;
}
- (NSArray *)imgArr {
    if (_imgArr == nil) {
        _imgArr = @[@"guide1",@"guide2",@"guide3",@"guide4"];
    }
    return _imgArr;
}

- (NSString *)getCurrentTimeStamp {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    NSString *ts = [NSString stringWithFormat:@"%ld",(long)timeSp];
    return ts;
}
- (long long)getDurationStartTime:(NSString *)startTime endTime:(NSString *)endTime {
    if (startTime && endTime) {
        long long aTime = [endTime longLongValue] - [startTime longLongValue];
        return aTime;
    } else {
        return -1;
    }
}
@end

推荐阅读更多精彩内容