UIScrollView

滚动 播放

contentSize //定义内容区域大小,决定是否能滚动
contentOffset //食欲左上角距离坐标远点的偏移量
scrollsToTop //滑动都顶部(点状态条的时候)
pagingEnabled //是否能整屏翻动
bounces //边界是否回弹


//在这里我们打印一下滚动视图的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 //指定页面的个数

#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;
 posted on 2015-04-30 07:45  ianhao_cn  阅读(219)  评论(0编辑  收藏  举报