在項(xiàng)目開發(fā)中断箫,很多都需要自定義引導(dǎo)頁,讓app更有特性秋冰。今天來說說自定義動(dòng)畫仲义,先上圖。
主要用了2種動(dòng)畫方式剑勾,位移和縮放埃撵。
1、scrollView的contentSize給pageCount*screenWidth的寬度
2甥材、pagingEnabled賦值為YES盯另,按頁滑動(dòng)
3、每一個(gè)LeadPageView中定義相應(yīng)的控件布局
4洲赵、定義屬性beginContentOffsetX鸳惯,記錄每一頁開始的contentOffsetX
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
self.beginContentOffsetX = scrollView.contentOffset.x;
}
5、根據(jù)scrollView的contentOffsetX與每一頁的beginContentOffsetX值變化叠萍,計(jì)算當(dāng)前滑動(dòng)的位置芝发,進(jìn)行PageView內(nèi)控件的位移變化或縮放
- 注意:正向滑動(dòng)與反向滑動(dòng)都要考慮到位移變化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger a=scrollView.contentOffset.x;
NSInteger c=LBScreenW;
CGFloat b=a%c;
CGFloat rate = (b/LBScreenW);
if (rate != 0 && scrollView.contentOffset.x > 0) {
if (scrollView.contentOffset.x - self.beginContentOffsetX > 0) {
/* 從左到右滑動(dòng) */
if (self.scrollView.contentOffset.x > LBScreenW ){
// 從第二到第三張
[self scrollPageTwoAnimate:rate];
} else if (scrollView.contentOffset.x > 0) {
// 從第一到第二張
[self scrollPageOneAnimate:rate];
}
} else {
/* 從右到左滑動(dòng) */
if (scrollView.contentOffset.x < LBScreenW) {
// 從第二到第一張
[self scrollPageOneAnimate:rate];
} else if (scrollView.contentOffset.x < LBScreenW * 2) {
// 從第三到第二張
[self scrollPageTwoAnimate:rate];
}
}
}
}
6、開啟按鈕苛谷,block事件回調(diào)
- (instancetype)initWithClickInsertApp:(void(^)(void))block {
if (self == [super init]) {
self.callback = block;
}
return self;
}
- (void)inserAppAction {
if (self.callback) {
self.callback();
}
}
7辅鲸、 Appdelegaet的didFinishLaunchingWithOptions中調(diào)用
__weak __typeof(self) weakSelf = self;
LBLeadPageController* vc = [[LBLeadPageController alloc] initWithClickInsertApp:^{
dispatch_async(dispatch_get_main_queue(), ^{ // 主線程更新
[weakSelf restoreRootViewController:[ViewController new]];
});
}];
self.window.rootViewController = vc;
8、更換rootViewController動(dòng)畫
// 更換rootViewController動(dòng)畫
- (void)restoreRootViewController:(UIViewController *)rootViewController {
typedef void (^Animation)(void);
UIWindow* window = [UIApplication sharedApplication].keyWindow;
rootViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
Animation animation = ^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[UIApplication sharedApplication].keyWindow.rootViewController = nav;
[UIView setAnimationsEnabled:oldState];
};
[UIView transitionWithView:window
duration:1.0f
options:UIViewAnimationOptionTransitionCurlUp
animations:animation
completion:nil];
}