iOS滚动视图UIScrollView

iOS滚动视图相关代码如下:

#import "TRRootAViewController.h"
#import "TRrootBViewController.h"
#define SCROLL_W 320
#define SCROLL_h 568-64

@interface TRRootAViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scroll;
@property (nonatomic,strong)UILabel *titlelabel;
@property(nonatomic,strong)TRrootBViewController *bvc;
@end

@implementation TRRootAViewController

#pragma mark 状态监测
//当滚动视图停止下来 得到一个通知 滚动视图就会用contentoddset反映出滚动视图的最终的位置
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"offset.x=%d",(int)scrollView.contentOffset.x);
}
//用户手指在屏幕上向某个方向拖动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"水平或竖直方向拖动");
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"拖动结束");
}
//用户的手指已经从屏幕上移开而视图需要继续移动调用这个方法
//从用户手指抬前的位置
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"将要停止的时候调用");
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"拖动结束");
}

-(void)creatUIscrollView
{
    self.scroll = [[UIScrollView alloc]init]; //初始化
    self.scroll.frame = CGRectMake(0, 0, 320, 568); //视图大小
    self.scroll.contentSize = CGSizeMake(SCROLL_W*6, SCROLL_h); //滚动范围
    self.scroll.pagingEnabled = YES;//分页
    self.scroll.bounces = YES; //弹簧
    self.scroll.delegate = self;  //协议
    self.scroll.userInteractionEnabled=YES;//用户交互
    [self.view addSubview:self.scroll];
    
    //给每个imgView添加图片
    for (int i=0; i<6; i++)
    {
        UIImage *img=[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]];
        UIImageView *imgview=[[UIImageView alloc]initWithImage:img];
        imgview.frame=CGRectMake(SCROLL_W * i, 0, SCROLL_W, SCROLL_h);//设置imgview的frame
        [self.scroll addSubview:imgview];
    }
    
    UIButton *prebtn = [UIButton buttonWithType:UIButtonTypeSystem];
    prebtn.backgroundColor = [UIColor redColor];
    prebtn.tag=101;
    prebtn.frame = CGRectMake(20, 420, 50, 40);
    [prebtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:prebtn];
    
    UIButton *hombtn=[UIButton buttonWithType:UIButtonTypeSystem];
    hombtn.backgroundColor=[UIColor redColor];
    hombtn.tag=102;
    hombtn.frame=CGRectMake(160-20, 420, 50, 40);
    [hombtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:hombtn];
    
    UIButton *nextbtn=[UIButton buttonWithType:UIButtonTypeSystem];
    nextbtn.backgroundColor=[UIColor redColor];
    nextbtn.tag=103;
    nextbtn.frame=CGRectMake(320-65, 420, 50, 40);
    [nextbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextbtn];
    
    self.titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(160, 64, 80, 40)];
    self.titlelabel.text=@"第1页";
    [self.view addSubview:self.titlelabel];
}

-(void)btnClick:(UIButton *)btn{
    
    int currentlndex=self.scroll.contentOffset.x/320;
    
    if (103==btn.tag) {                                               
        
        NSLog(@"offset.x%f",self.scroll.contentOffset.x);
        
        if (self.scroll.contentOffset.x<=4*320) {
            [self.scroll setContentOffset:CGPointMake(self.scroll.contentOffset.x+320, 0) animated:YES];
            currentlndex++;
        }

    }
    else if(101==btn.tag){
        
        if (self.scroll.contentOffset.x>=320) {
            [self.scroll setContentOffset:CGPointMake(self.scroll.contentOffset.x-320, 0) animated:YES];
            currentlndex--;
        }
    }
    else if(102==btn.tag){

        [self.scroll setContentOffset:CGPointMake(0, 0) animated:YES];
        currentlndex=0;
        
    }
    self.titlelabel.text=[NSString stringWithFormat:@"第%d页",currentlndex+1];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatUIscrollView];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值