UIScrollView

目錄
   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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容