【IOS】- ScrollView-01

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   使用   sp   for   

•CGSize contentSize: 設定UIScrollView的滾動範圍
•CGPoint contentOffset: UIScrollView當前滾動的位置
•UIEdgeInsets contentInset: 增加滾動視圖四周的增加滾動範圍

1.建立

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];    [self.view addSubview:scrollView];

2.添加imageview

    UIImage *image = [UIImage imageNamed:@"001.jpeg"];    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];        [scrollView addSubview:imageView];    // 如果不指定uiimageView 大小預設會使用image的大小

3.設定滾動屬性

 // 2. 設定滾動視圖屬性    [scrollView setContentSize:image.size];

4.設定邊界屬性

  // 設定邊界屬性    UIEdgeInsets edgs = UIEdgeInsetsMake(10, 10, 10, 10);// 上左下右    [scrollView setContentInset:edgs];

5.設定是否擁有彈簧屬性

  [_scrollView setBounces:NO];

6.設定橫縱捲軸

    [_scrollView setShowsHorizontalScrollIndicator:NO];    [_scrollView setShowsVerticalScrollIndicator:NO];

7.設定縮小與放大

    // 指定最小縮放比例    [_scrollView setMinimumZoomScale:0.2];    // 指定最大縮放比例    [_scrollView setMaximumZoomScale:2.0];    // 設定滾動視圖的代理    [_scrollView setDelegate:self];

8.縮放時執行的方法

#pragma mark 滾動視圖的代理方法// 縮放中的代理方法- (void)scrollViewDidZoom:(UIScrollView *)scrollView{    NSLog(@"縮放中。。。");}

9.縮放完成執行的代理方法

// 縮放完成的代理方法- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{    NSLog(@"縮放完成 %f", scale);    // 換個映像試試看    NSString *imageFile = [NSString stringWithFormat:@"%03d.jpeg", arc4random_uniform(10) + 1];    UIImage *image = [UIImage imageNamed:imageFile];        [_imageView setImage:image];}

10.返回要縮放的視圖對象

// 一定要記住:本代理方法的傳回值就是“要縮放的視圖對象”- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return _imageView;}

11.移動映像的方法

- (IBAction)moveImage:(UIButton *)sender{    CGPoint offset = _scrollView.contentOffset;        switch (sender.tag) {        case 0:            offset.x -= 50;            break;        case 1:            offset.y -= 50;            break;        case 2:            offset.y += 50;            break;        case 3:            offset.x += 50;            break;        default:            break;    }        // 需要做一個位置的修正    // 水平方向,注意在修正左邊位置時,需要使用edge的-left    UIEdgeInsets edge = _scrollView.contentInset;    if (offset.x < -edge.left) {        offset.x = -edge.left;    } else if (offset.x > _scrollView.contentSize.width - _scrollView.bounds.size.width + edge.right) {        offset.x = _scrollView.contentSize.width - _scrollView.bounds.size.width + edge.right;    }        // 垂直方向,注意在修正頂部位置時,需要使用edge的-top    if (offset.y < -edge.top) {        offset.y = -edge.top;    } else if (offset.y > _scrollView.contentSize.height - _scrollView.bounds.size.height + edge.bottom) {        offset.y = _scrollView.contentSize.height - _scrollView.bounds.size.height + edge.bottom;    }        [_scrollView setContentOffset:offset animated:YES];

12.增加移動動畫

    // 修改映像的便宜位置    [UIView animateWithDuration:0.3f animations:     ^{         [_scrollView setContentOffset:offset];     }];

 

【IOS】- ScrollView-01

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.