UIScrollView 基础学习 - UI

#import "PointViewController.h"

@interface PointViewController ()

@end

@implementation PointViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建  scrollView
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    scrollView.backgroundColor = [UIColor cyanColor];
    
    //切记 : 设置滚动区域
    //contentSize 默认和scrollView的size一样大
    //只有当contentSize的宽大于scrollview的宽时,横向才能滚动
    //同理,只有contentSize的高大于scrollView的高时,竖向才能滚动
    
    scrollView.contentSize = CGSizeMake(2560, 480);

    
    //创建imageView显示图像
    
//    UIImage *image1 = [UIImage imageNamed:@"image1.jpg"];
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
    
    //将滚动区域设置为图片大小
    
//    scrollView.contentSize = image1.size;
    
//    [scrollView addSubview:imageView];
    
    //实现类似相册的效果,显示3张图片
    //创建3个imageView,分别显示三张图片
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    imageView1.image = [UIImage imageNamed:@"image1.jpg"];
    [scrollView addSubview:imageView1];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
    imageView2.image = [UIImage imageNamed:@"image2.jpg"];
    [scrollView addSubview:imageView2];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(640, 0, 320, 480)];
    imageView3.image = [UIImage imageNamed:@"image3.png"];
    [scrollView addSubview:imageView3];
    
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(960, 0, 320, 480)];
    imageView4.image = [UIImage imageNamed:@"image4.jpg"];
    [scrollView addSubview:imageView4];
    
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(1280, 0, 320, 480)];
    imageView5.image = [UIImage imageNamed:@"image5.jpg"];
    [scrollView addSubview:imageView5];
    
    UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(1600, 0, 320, 480)];
    imageView6.image = [UIImage imageNamed:@"image6.jpeg"];
    [scrollView addSubview:imageView6];
    
    UIImageView *imageView7 = [[UIImageView alloc] initWithFrame:CGRectMake(1920, 0, 320, 480)];
    imageView7.image = [UIImage imageNamed:@"image7.jpeg"];
    [scrollView addSubview:imageView7];
    
    UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(2240, 0, 320, 480)];
    imageView8.image = [UIImage imageNamed:@"image8.jpeg"];
    [scrollView addSubview:imageView8];
    //设置分页效果
    //scrollView分页属性默认为NO;
    
    scrollView.pagingEnabled = YES;
    
    //scrollView设置是否回弹;
    //默认YES;
    scrollView.bounces = YES;
    
    //scrollView设置偏移量contentOffset
    //手动修改contenOffset,影响视图位置(显示第2张)
    //当视图移动过程中,contentOffset一直在变化,视图像左移动,偏移量增加,向右移动,偏移量减少.
    
    scrollView.contentOffset = CGPointMake(0, 0);

    //设置scrollView是否能够滑动
//    scrollView.scrollEnabled = NO;

    
    [self.view addSubview:scrollView];
    //是否显示横向的滑动条
    scrollView.showsHorizontalScrollIndicator = NO;
    //是否显示纵向的滑动条
    scrollView.showsVerticalScrollIndicator = NO;
    //
    scrollView.delegate = self;
    
    [scrollView release];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
// any offset changes
{
//    NSLog(@"--------滑动中%s %d",__FUNCTION__,__LINE__);
    NSLog(@"contentOffset = %@",NSStringFromCGPoint(scrollView.contentOffset));
//    NSLog(@"--------------- %.0f---------------",scrollView.contentOffset.x / 320 + 1);
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2) // any zoom scale changes
{
//    NSLog(@"--------缩放%s %d",__FUNCTION__,__LINE__);
}
// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
{
//    NSLog(@"--------将要开始拖拽%s %d",__FUNCTION__,__LINE__);
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0)
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
{
//    NSLog(@"--------将要结束拖拽%s %d",__FUNCTION__,__LINE__);
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
//    NSLog(@"--------结束拖拽%s %d",__FUNCTION__,__LINE__);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //计算scrollView停止滚动时,停留在第几页
    NSLog(@"--------------- %.0f---------------",scrollView.contentOffset.x / 320 + 1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值