美文网首页
UIScrollView

UIScrollView

作者: L柠_檬 | 来源:发表于2016-08-19 14:12 被阅读31次
    目录
       1.1 引导页实现
       1.2 轮播图实现
    
    1.1  引导页实现
    
    #import <UIKit/UIKit.h>
    
    block 回调点击事件
    typedef void(^BlockBackResetMessage)();
    
    @interface DidDisplayPageViewController : UIViewController
    
    //进入首页提示展示图
    @property (nonatomic ,copy)BlockBackResetMessage blockBackResetMessage;
    
    @end
    
    #import "DidDisplayPageViewController.h"
    
    //宏定义
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width //屏幕宽度
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height //屏幕高度
    
    #define HEIGHT4 480
    #define HEIGHT5 568
    #define HEIGHT6 667
    #define HEIGHT6P 736
    
    @interface DidDisplayPageViewController ()
    
    @property (nonatomic ,strong)UIScrollView *mainScrollView;
    
    @property (nonatomic ,strong)UIPageControl *mainPageControl;
    
    @end
    
    @implementation DidDisplayPageViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        
        // Do any additional setup after loading the view.
        
        [self createScrollView];
        
        [self createQuiteButton];
        
        [self createPageControl];
        
    }
    
    
    - (void)createScrollView{
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
        self.mainScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        self.mainScrollView.delegate = self;
        
        self.mainScrollView.showsHorizontalScrollIndicator = NO;
        
        NSArray *array=[NSArray arrayWithObjects:@"引导页1",@"引导页2",@"引导页3", nil];
        
        for (int i=0; i < 3; i++) {
            
            UIImage *image = [UIImage imageNamed:[array objectAtIndex:i]];
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:
                                      CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            
            imageView.image=image;
            
            [self.mainScrollView addSubview:imageView];
            
        }
        
        //按页滚动
        self.mainScrollView.pagingEnabled=YES;
        
        //偏移量
        self.mainScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*3, SCREEN_HEIGHT);
        
        //竖直滚动条隐藏
        self.mainScrollView.showsHorizontalScrollIndicator=NO;
        
        默认YES,一般为NO,否者有颤抖
        self.mainScrollView.bounces = NO;
        
        [self.view addSubview: self.mainScrollView];
        
        
    }
    
    处理大雨或者小于时候的滑动
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        
        if (self.mainScrollView.contentOffset.x <= 0) {
            
            self.mainScrollView.contentOffset= CGPointMake(0, 0);
            
        }
        
        if (self.mainScrollView.contentOffset.x >= SCREEN_WIDTH*2) {
            
            self.mainScrollView.contentOffset= CGPointMake(SCREEN_WIDTH*2, 0);
            
        }
        
    }
    
    
    - (void)createQuiteButton{
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.mainScrollView.userInteractionEnabled=YES;
        
        if (SCREEN_HEIGHT ==HEIGHT5) {
            
            button.frame = CGRectMake(SCREEN_WIDTH*2+(SCREEN_WIDTH - 145)/2.0f,
                                      SCREEN_HEIGHT-120, 145, 35);
            
        }else{
            
            button.frame = CGRectMake(SCREEN_WIDTH*2+(SCREEN_WIDTH - 145)/2.0f,
                                      SCREEN_HEIGHT-140, 145, 35);
            
        }
        
        [button setImage:[UIImage imageNamed:@"display"] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(removeQuiteButton)
         forControlEvents:UIControlEventTouchUpInside];
        
        [self.mainScrollView addSubview:button];
        
    }
    
    
    - (void)removeQuiteButton{
        
        [[NSUserDefaults standardUserDefaults]setObject:@(YES) forKey:@"DisplayPage"];
        
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        if(self.blockBackResetMessage){
            
            self.blockBackResetMessage();
            
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    
    
    //创建滚动偏移点
    - (void)createPageControl{
        
        self.mainPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT - 65,SCREEN_WIDTH,20)];
        
        self.mainPageControl.numberOfPages = 3;
        
        self.mainPageControl.currentPage = 0;
        
        self.mainPageControl.pageIndicatorTintColor = [JDSiliconValley colorWithRGBValue:0xe8e8e8];
        
        self.mainPageControl.currentPageIndicatorTintColor = [JDSiliconValley colorWithRGBValue:0xd1d1d1];
        
        
        self.mainPageControl.userInteractionEnabled = YES;
        
        [self.view addSubview:self.mainPageControl];
        
    }
    
    
    滚动视图和PageControl联动
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        
        NSLog(@"x:%f",scrollView.contentOffset.x);
        
        NSLog(@"y:%f",scrollView.contentOffset.y);
        
        CGPoint pt = scrollView.contentOffset;
        
        //通过scrollView的偏移量,更新⻚页码显⽰示
        
        self.mainPageControl.currentPage = pt.x/SCREEN_WIDTH;
        
    }
    
    
    displayPage2.png

    相关文章

      网友评论

          本文标题:UIScrollView

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