美文网首页iOSiOS 引导页
iOS编程,怎么加引导页

iOS编程,怎么加引导页

作者: 霍伟健 | 来源:发表于2016-02-27 20:37 被阅读1134次

    在AppDelegate中实现。

    在AppDelegate中所有控件创建完成之后, 写引导页代码。注意:引导页在第一次进入软件的时候才会出现,因此每次进入运行的时候应该先从沙盒中读取,判断沙盒中是否含有文件, 没有文件的时候创建一个文件,以便下次运行的时候就不再出现引导页。具体代码如下。

    /**  核心代码 */
    #pragma mark ** 判断是否第一次进入
    - (void)createGuidancePage {
        
        NSLog(@"%@", NSHomeDirectory());
        // 第一次进入, 从沙盒中读文件.
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"引导页"]) {
            // 沙盒中没有, 写一个文件.
            [[NSUserDefaults standardUserDefaults] setBool:1 forKey:@"引导页"];
            
            NSArray *arrayOfImageView = [NSArray array];
            
            //添加引导页图片
            arrayOfImageView = @[@"yindaoye1", @"yindaoye2", @"yindaoye3"];
            
            self.scrollViewForGuidancePage = [[UIScrollView alloc] init];
            
            //注意:添加在window上
            [self.window addSubview:self.scrollViewForGuidancePage];
            
            [_scrollViewForGuidancePage release];
            
            self.scrollViewForGuidancePage.bounces = NO;
            
            self.scrollViewForGuidancePage.pagingEnabled = YES;
            
            self.scrollViewForGuidancePage.showsHorizontalScrollIndicator = NO;
            
            self.scrollViewForGuidancePage.userInteractionEnabled = YES;
            
            self.scrollViewForGuidancePage.frame = self.window.frame;
            
            self.scrollViewForGuidancePage.contentSize = CGSizeMake(self.window.frame.size.width * arrayOfImageView.count, 0);
            
            self.scrollViewForGuidancePage.delegate = self;
            
            self.pageControlForGuidancePage = [[UIPageControl alloc] init];
            
            [self.window addSubview:self.pageControlForGuidancePage];
            
            [_pageControlForGuidancePage release];
            
            self.pageControlForGuidancePage.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
            
            self.pageControlForGuidancePage.center = CGPointMake(self.scrollViewForGuidancePage.frame.size.width / 2, self.scrollViewForGuidancePage.frame.size.height - 40);
            
            self.pageControlForGuidancePage.numberOfPages = arrayOfImageView.count;
            
            //for循环, 在scrollView上添加照片
            for (int i = 0; i < arrayOfImageView.count; i++) {
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arrayOfImageView[i]]];
                
                imageView.frame = CGRectMake(self.scrollViewForGuidancePage.frame.size.width * i, 0, self.scrollViewForGuidancePage.frame.size.width, self.scrollViewForGuidancePage.frame.size.height);
                
                [self.scrollViewForGuidancePage addSubview:imageView];
                
                imageView.userInteractionEnabled = YES;
                
                if (i == arrayOfImageView.count - 1) {
                    
                    //引导页最后一页"开始体验"按钮, 进入APP
                    self.buttonForStart = [UIButton buttonWithType:UIButtonTypeCustom];
                    
                    [imageView addSubview:self.buttonForStart];
                    
                    [self.buttonForStart setTitle:@"开始体验" forState:UIControlStateNormal];
                    
                    self.buttonForStart.layer.borderWidth = 1;
                    
                    self.buttonForStart.layer.borderColor = [[UIColor whiteColor] CGColor];
                    
                    self.buttonForStart.layer.cornerRadius = 7;
                    
                    self.buttonForStart.layer.masksToBounds = YES;
                    
                    self.buttonForStart.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
                    
                    self.buttonForStart.center = CGPointMake(self.window.frame.size.width / 2, self.window.frame.size.height - 100);
                    
                    [self.buttonForStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
                    
                }
                
            }
            
        }
        
    }
    
    

    /** button 的点击事件,点击之后进入主页. */

    
    - (void)start {
        
        //从父视图上移除
        [self.pageControlForGuidancePage removeFromSuperview];
        
        [self.scrollViewForGuidancePage removeFromSuperview];
        
    }
    
    

    /** scrollView 的Delegate, 让pageControl跟着scrollView的contentOffset的变化而变化. */

    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        
        self.pageControlForGuidancePage.currentPage = self.scrollViewForGuidancePage.contentOffset.x / self.window.frame.size.width;
        
    }
    
    

    相关文章

      网友评论

      本文标题:iOS编程,怎么加引导页

      本文链接:https://www.haomeiwen.com/subject/smwwkttx.html