#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
一個拖拽圖片漸變切換的啟動頁
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 本文為僅學習記錄 參考項目鏈接GitHub - Bakumon/UGank: 「有干貨」Gank.io 第三方客戶...
- 場景: 業務需要,在app的啟動頁面做一個模版,可以將廣告圖片直接插入進去.......適合啟動頁廣告、相框 思...