UIScrollView - 点击跳转

#define Screen_Width    [[UIScreen mainScreen] bounds].size.width
#define Screen_Height   [[UIScreen mainScreen] bounds].size.height
@implementation ViewController {
    UILabel *_indexLabel;
    UIScrollView *_scrollView;
    UIScrollView *scrollView1;
    BOOL _isHidden;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _isHidden = YES;
    
    
    
//    UIDevice *device = [UIDevice currentDevice];
//    
//    NSString *name = device.systemName;
//    NSLog(@"name = %@",name);
//    

    
// 本视图控制器会 自动调整scrollView的插入
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.view.backgroundColor =[UIColor whiteColor];
    
    // scrollView
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
    _scrollView.backgroundColor = [UIColor lightGrayColor];
    _scrollView.contentSize = CGSizeMake(Screen_Width*6, Screen_Height);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    for (int i = 0; i < 6; i++) {
        
        
        // imageView
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.tag = 100+i;
        imageView.frame = CGRectMake(Screen_Width*i, 0, Screen_Width, Screen_Height);
//        imageView.tag = i+1;
        [_scrollView addSubview:imageView];
        
        
        // 打开imageView的用户交互
//        A Boolean value that determines whether user events are ignored and removed from the event queue. 返回一个BOOL值 判断是否用户事件被忽略或者从事件队列中移除
        imageView.userInteractionEnabled = YES;
        
        // 点击手势 视图可以加手势
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        
        // 设置点击次数
//        The number of taps for the gesture to be recognized.
        // 手势点击次数识别
        tapGestureRecognizer.numberOfTapsRequired = 1;
        
        // 手指数
//        The number of fingers required to tap for the gesture to be recognized.
        // 手势手指数量点击识别
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        
        // 将手势加给imageView  Attaches a gesture recognizer to the view. 将一个手势识别绑定在视图上
        [imageView addGestureRecognizer:tapGestureRecognizer];
    }
    
    // label
    _indexLabel = [[UILabel alloc]init];
    _indexLabel.text = [NSString stringWithFormat:@"1"];
    _indexLabel.frame = CGRectMake(10, 20, 300, 30);
    _indexLabel.font = [UIFont boldSystemFontOfSize:25];
    _indexLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_indexLabel];
    
    // label在scrollView之后加上
    
    scrollView1 = [[UIScrollView alloc] init];
    scrollView1.frame = CGRectMake(-80, 0, 80, 480);
    scrollView1.pagingEnabled = YES;
    scrollView1.contentSize = CGSizeMake(80, 160*6);
    
    
   
}

- (void)tapClick:(UITapGestureRecognizer *)tapGettureRecognizer {
    
    _isHidden = !_isHidden;
    // 80 * 160
    for (int i = 0; i < 6; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];
        imageView.frame = CGRectMake(0, 160*i, 100, 160);
        [scrollView1 addSubview:imageView];
        
        imageView.tag = i;
        // 必须开始UIImageView的用户交互,不然无法响应点击事件
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePic:)];
        
        tap.numberOfTapsRequired = 1;
        
        // 手指数 The number of fingers required to tap for the gesture to be recognized.
        // 手势手指数量点击识别
        tap.numberOfTouchesRequired = 1;
        
        // 将手势加给imageView
        [imageView addGestureRecognizer:tap];
        
    }
    [self.view addSubview:scrollView1];
    if (_isHidden) {
        [UIView animateWithDuration:0.5 animations:^{
            scrollView1.frame = CGRectMake(-80, 0, 80, 480);
        } completion:nil];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            scrollView1.frame = CGRectMake(0, 0, 80, 480);
        } completion:nil];
    }
}

- (void)changePic:(UITapGestureRecognizer *)tap {
   NSInteger index =  tap.view.tag;
    _scrollView.contentOffset = CGPointMake(Screen_Width*index, 0);
    _indexLabel.text = [NSString stringWithFormat:@"%ld",index+1];
    
  
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 通过偏移量得到在第几页
    int index = [scrollView contentOffset].x/Screen_Width;
    _indexLabel.text = [NSString stringWithFormat:@"%d",index+1];
}
<pre name="code" class="objc">当点击事件执行时,不会响应UIScrollView对象的代理事件,因为只是偏移量改变了,没有触发滑动减速的事件。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值