• Day9


    1 UIScrollView 与 delegate的通讯

       1.1 用户开始拖拽时,调用scrollViewWillBeginDragging

       1.2 具体滚动到某个位置,调用scrollViewDidScroll

       1.3 调用scrollViewDidEndDragging:willDecelerate 

    (1) 声明协议 <UIScrollViewDelegate>

    (2) 设置代理

    (3) 实现对应的方法

    //
    //  ViewController.m
    //  
    //
    //  Created by xin on 15-4-4.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UIButton *lastContent;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        CGFloat height = CGRectGetMaxY(self.lastContent.frame)+44.f;
        self.scrollView.contentSize = CGSizeMake(0, height);
        
        //声明协议
        self.scrollView.delegate = self;
    }
    
    //设置代理
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        //实现方法
        NSLog(@"start dragging");
    }
    
    
    @end
    

      

    2 用户使用捏合手势时调用

      viewForZoomingInScrollView

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        CGFloat height = CGRectGetMaxY(self.lastContent.frame)+44.f;
        self.scrollView.contentSize = CGSizeMake(0, height);
        
        //声明协议
        self.scrollView.delegate = self;
        
        //定义缩放的范围
        self.scrollView.maximumZoomScale = 2.f;
        self.scrollView.minimumZoomScale = .5f;
    }
    
    //返回uiview
    -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        return self.imageView;
    }
    

      

    3 pageControl 的2个属性

    self.pageControl.numberOfPages = ImageCount;
    self.pageControl.currentPage = pageNum;
    

    4 scrollView的图片滚动需要注意的点

    // 让动画一页一页的效果
        self.scrollView.pagingEnabled = YES;
    
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGPoint offSet = self.scrollView.contentOffset;
        CGFloat offsetX = offSet.x;
        CGFloat width = self.scrollView.frame.size.width;
        //加多0.5width,提前预判
        int pageNum = (offsetX + 0.5f * width) / width ;
        
        self.pageControl.currentPage = pageNum;
    }
    

    所有的code

    //
    //  ViewController.m
    //  scrollPicture
    //
    //  Created by xin on 15/4/7.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #define ImageCount 5
    
    @interface ViewController ()<UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        CGFloat width = self.scrollView.bounds.size.width;
        CGFloat height = self.scrollView.bounds.size.height;
        //image draw
        for (int i=0; i<ImageCount+1; i++) {
            UIImageView *imageView = [[UIImageView alloc]init];
            CGFloat imageX = (i-1)*width;
            imageView.frame = CGRectMake(imageX, 0.f, width, height);
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i]];
            [self.scrollView addSubview:imageView];
            
        }
        
        self.scrollView.contentSize = CGSizeMake((ImageCount*width), 0);
        // 让动画一页一页的效果
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        self.pageControl.numberOfPages = ImageCount;
        
    }
    
    #pragma mark - UIScrollViewDelegate实现方法
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGPoint offSet = self.scrollView.contentOffset;
        CGFloat offsetX = offSet.x;
        CGFloat width = self.scrollView.frame.size.width;
        //加多0.5width,提前预判
        int pageNum = (offsetX + 0.5f * width) / width ;
        
        self.pageControl.currentPage = pageNum;
    }
    
    
    @end
    

      

    NSTimer

    //usage
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    //invalidate
    [self.timer invalidate];
        self.timer =  nil;
    

     上面的写法会引发冲突,如果存在其它的dragging事件

    所以

    self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    

      

     

  • 相关阅读:
    【字符串处理算法】字符串包含的算法设计及C代码实现【转】
    linux中字符串转换函数 simple_strtoul【转】
    Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
    linux非阻塞的socket EAGAIN的错误处理【转】
    嵌入式 uboot引导kernel,kernel引导fs【转】
    jQuery入门(4)jQuery中的Ajax应用
    jQuery入门(3)事件与事件对象
    jQuery入门(2)使用jQuery操作元素的属性与样式
    jQuery入门(1)jQuery中万能的选择器
    JavaScript在IE6下超级链接window.location.href不跳转的bug 及 解决方案
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4399210.html
Copyright © 2020-2023  润新知