iOS 引导页动画LeadPageControl

在项目开发中,很多都需要自定义引导页,让app更有特性。今天来说说自定义动画,先上图。

LeadPageControl.gif

主要用了2种动画方式,位移和缩放。

1、scrollView的contentSize给pageCount*screenWidth的宽度

2、pagingEnabled赋值为YES,按页滑动

3、每一个LeadPageView中定义相应的控件布局

4、定义属性beginContentOffsetX,记录每一页开始的contentOffsetX

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.beginContentOffsetX = scrollView.contentOffset.x;
}

5、根据scrollView的contentOffsetX与每一页的beginContentOffsetX值变化,计算当前滑动的位置,进行PageView内控件的位移变化或缩放

  • 注意:正向滑动与反向滑动都要考虑到位移变化
- (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) {
            /* 从左到右滑动 */
            if (self.scrollView.contentOffset.x > LBScreenW ){
                // 从第二到第三张
                 [self scrollPageTwoAnimate:rate];
            } else if (scrollView.contentOffset.x > 0) {
                // 从第一到第二张
                 [self scrollPageOneAnimate:rate];
            }
        } else {
            /* 从右到左滑动 */
            if (scrollView.contentOffset.x < LBScreenW) {
                // 从第二到第一张
                 [self scrollPageOneAnimate:rate];
            } else if (scrollView.contentOffset.x < LBScreenW * 2) {
                // 从第三到第二张
                 [self scrollPageTwoAnimate:rate];
            }
        }
    }
}

6、开启按钮,block事件回调

- (instancetype)initWithClickInsertApp:(void(^)(void))block {
    if (self == [super init]) {
        self.callback = block;
    }
    return self;
}
- (void)inserAppAction {
    if (self.callback) {
        self.callback();
    }
}

7、 Appdelegaet的didFinishLaunchingWithOptions中调用

    __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动画

// 更换rootViewController动画
- (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];
}

Demo传送门

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容