IOS学习 scrollView的基本属性

来源:互联网 发布:胜利足彩欧赔数据库 编辑:程序博客网 时间:2024/06/11 09:22

//总共图片数量

#define kIMGCOUNT 4

#define VIEW_WIDTH self.view.frame.size.width

#define VIEW_HEIGHT self.view.bounds.size.height

@interface HomeViewController :UIViewController<UIScrollViewDelegate>

{

    UIImageView *imageView;

@private

    UIScrollView *_scrollView;

}




@implementation HomeViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    [selfinitViewController];

}


- (void)initViewController

{    

    //设置scrollview

    _scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,20,VIEW_WIDTH ,261)];  //屏幕宽度

//    scrollView.backgroundColor = [UIColor redColor];

    

    [self.viewaddSubview:_scrollView];


    //设置内容大小

    _scrollView.contentSize =CGSizeMake(VIEW_WIDTH*kIMGCOUNT,VIEW_HEIGHT*5);

    

//    _scrollView.pagingEnabled = YES;   //分页效果

    

    //隐藏滚动指示条

//    _scrollView.showsHorizontalScrollIndicator = YES;

    //    scrollView.showsVerticalScrollIndicator = YES;

    

    //设置图片

    for (int i =0; i<kIMGCOUNT; i++)

    {

        imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width*i,0, self.view.frame.size.width,261)];

        //设置图片

        NSString *name = [NSStringstringWithFormat:@"background%d",i+1];

        imageView.tag = i;

        imageView.image=[UIImageimageNamed:name];

        [_scrollViewaddSubview:imageView];

    }

  

    //设置代理

    _scrollView.delegate=self;

    

    //图片缩放的最大最小值设置

//    _scrollView.minimumZoomScale = 0.3;

//    _scrollView.maximumZoomScale = 3;


    // 用来记录scrollview滚动的位置

    // scrollView.contentOffset = ;

    

     //是否反弹效果,默认为反弹

//    _scrollView.bounces = NO;

    

    //是否滚动,默认可以滚动

//    _scrollView.scrollEnabled = NO;

    

    //设置indication滚动条风格默认//,三种风格

//    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    

    //提示用户,滚动条闪一下

//    [_scrollView flashScrollIndicators];

    

    // 增加额外的滚动区域(逆时针,上、左、下、右)

    // top  left  bottom  right  设置内容和滚动条的边缘

//    _scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);

//    _scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 50, 0);

    

    //滚动条是否可以同时运动

//    _scrollView.directionalLockEnabled = YES;

    

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(VIEW_WIDTH,210, VIEW_WIDTH,40)];

    label.text =@"学习scrollView";

    label.backgroundColor = [UIColoryellowColor];

    [_scrollView addSubview:label];

        

    UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50,350, 100, 40)];

    [btn setTitle:@"滚动"forState:UIControlStateNormal];

    btn.backgroundColor = [UIColorpurpleColor];

    [btn addTarget:selfaction:@selector(test)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

    

}


- (void)test{

    //将内容移动到指定位置

    [_scrollViewsetContentOffset:CGPointMake(VIEW_WIDTH,0) animated:YES]; //只改变了x,y的值

//    [_scrollView scrollRectToVisible:CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, 500) animated:YES];


}


0 0