[7]UIScrollView,UIPageControl

UIScrollView

滾動 播放

contentSize //定義內容區域大小,決定是否能滾動
contentOffset //食慾左上角距離座標遠點的偏移量
scrollsToTop //滑動都頂部(點狀態條的時候)
pagingEnabled //是否能整屏翻動
bounces //邊界是否回彈app

//在這裏咱們打印一下滾動視圖的contenSize,來看一下是否在縮放時,滾動視圖的contenSize發生了改變
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2);
{
    NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));
//    scrollView.contentSize = CGSizeMake(1150, 1000);

}
//viewDidLoad中的代碼
#pragma mark  ----------設置滾動視圖的滾動-----
    UIScrollView *sView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 350, 600)];
    sView.backgroundColor = [UIColor grayColor];
    sView.contentSize = CGSizeMake(1000, 800);//contentsize這個屬性決定了滾動視圖能顯示內容的區域的大小,也是能滾動區域的大小.
    sView.contentOffset = CGPointMake(250, 10); //視圖左上角距離座標原點的偏移量
    sView.scrollEnabled = YES;//讓scrollView回到頂部
//    sView.pagingEnabled = YES;//默認爲NO,是否整屏翻動 控制的時上下左右
    sView.bounces = YES;//設置當前活動邊界的時候是否回彈,默認是yes;
//    sView.alwaysBounceVertical = YES; //垂直 上下回彈
//    sView.alwaysBounceHorizontal = YES; //左右水平回彈
    
    //增長滑動區域,上下左右多處多少的活動區域
//    sView.contentIn
//    set = UIEdgeInsetsMake(110, 10, 10, 10);
#pragma mark      ----------設置滾動的視圖縮放屬性-------
    sView.maximumZoomScale = 10.0; //指定最大的放大倍數
    sView.minimumZoomScale = 0.5; //設置最小的縮小倍數,設定倍數以後是 不能進行縮放的,必須指定縮放的是誰
    sView.delegate = self;//將視圖控制器指定爲滾動視圖的代理對象,去負責指定須要縮放
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(250, 10, 150, 100)];
    label.userInteractionEnabled = YES; //交互 與滑動無關的
    label.text = @"滾動把\n 牛寶寶";
    label.numberOfLines = 0;//斷行
    label.backgroundColor = [UIColor whiteColor];
    [sView addSubview:label];
    [label release];
    [self.view addSubview:sView];
    [sView release];

UIPageControl

currentPage //當前頁
numberOfPages //指定頁面的個數dom

#pragma mark  ----------建立一個UIPageControl對象而且配置--------------
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(25, 630, 300, 20)];
    page.numberOfPages = 5; //給定多少頁
    page.currentPage = 2;   //指定初始頁,必須在給定頁數以後,否則不會發生改變
    page.backgroundColor = [UIColor blackColor];
    [page addTarget:self action:@selector(aa:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:page];

UIScrollView UIPageControl 實現輪迴播放

//.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic,retain) UIPageControl *page;
@property (nonatomic,retain) UIScrollView *sView;
@property (nonatomic,retain) UILabel *firstLabel;
@end

//.m文件中
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController


-(void)dealloc
{
    [_firstLabel release];
    [_sView release];
    [_page release];
    [super dealloc];
}



//在滾動方法中實現最後一張與第一張的臨界翻轉
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGFloat width = self.view.frame.size.width;
    if (offset.x + width > scrollView.contentSize.width ) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    else if(offset.x < 0 ){
        [scrollView setContentOffset:CGPointMake(10*width, 0) animated:NO];
    }

}


//在滾動視圖中止滾動的時候,改變pagecontroltPa的currenge的值
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //獲取當前滾動視圖的偏移量
    CGPoint offset = scrollView.contentOffset;
    //計算當前是第幾頁
    NSInteger page = offset.x / self.view.frame.size.width;
//    if (page == 9) {
//        _page.currentPage = 0;
//    }
//    else {
//    _page.currentPage  = page - 1;
//    }
    _page.currentPage = page - 1 < 0 ? 9 :page - 1;

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    UIScrollView *sView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    

    
    sView.contentSize = CGSizeMake(width*11, height);
    sView.pagingEnabled = YES;
    _sView.showsHorizontalScrollIndicator = NO;
    sView.delegate = self;
    _sView =sView;

    //爲了讓第一張圖顯示爲0,必須從新設置滾動視圖的偏移量
    _sView.contentOffset = CGPointMake(width, 0);
   
    
    
    [self.view addSubview:sView];
    
    NSArray *arrText = @[@"我是第一個",@"我是第二個",@"誰是第三個",@"你是第四個",@"他是第五個",@"第六個出來",@"喝了七個",@"再來八個",@"九九一杯酒",@"十全十美"];
    
    _firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    _firstLabel.text = @"十全十美";
    _firstLabel.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0      green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    _firstLabel.font = [UIFont boldSystemFontOfSize:30];
    _firstLabel.textAlignment = NSTextAlignmentCenter;
    [_sView addSubview:_firstLabel];
    
    
    for (int i = 0; i < 10; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(width*(i+1), 0, width, height)];
        if (i == 9) {
            label.backgroundColor = _firstLabel.backgroundColor;
        }
        else{
        label.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
        }
        label.text = arrText[i];
        label.font = [UIFont boldSystemFontOfSize:30];
        label.textAlignment = NSTextAlignmentCenter;
        [sView addSubview:label];
        [label release];
        
    }
    
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height-50, width, 30)];
//    page.backgroundColor = [UIColor blackColor];
    page.numberOfPages = 10;
    page.pageIndicatorTintColor = [UIColor redColor];
    page.currentPageIndicatorTintColor = [UIColor blackColor];
    page.currentPage = 0;
    [self.view addSubview:page];
    
    _page = page;
    
    [page release];
    [sView release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

小技巧

ScrollView莫名其妙不能在viewController劃到頂怎麼辦?

self.automaticallyAdjustsScrollViewInsets = NO;
相關文章
相關標籤/搜索