iOS_7_scrollView大圖縮放

來源:互聯網
上載者:User

標籤:ios   uiscrollview   

最終:



BeyondViewController.h

////  BeyondViewController.h//  7_scrollView大圖展示////  Created by beyond on 14-7-24.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController- (IBAction)upBtnClick:(id)sender;@end



BeyondViewController.m

////  BeyondViewController.m//  7_scrollView大圖展示////  Created by beyond on 14-7-24.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"@interface BeyondViewController ()<UIScrollViewDelegate>{    UIScrollView *_scrollView;        // 要縮放的是內部的哪一個控制項    UIImageView *_imgView;}@end@implementation BeyondViewController- (void)viewDidLoad{    [super viewDidLoad];    // UIImageView    // 1,類方法建立控制項    // UIImageView *imgView = [[UIImageView alloc]init];    // 2,控制項細節//    NSString *imgName = [NSString stringWithFormat:@"HD.jpg"];//    imgView.image = [UIImage imageNamed:imgName];//    CGFloat imgW = imgView.image.size.width;//    CGFloat imgH = imgView.image.size.height;//    imgView.frame = CGRectMake(0, 0, imgW, imgH);        // 或者快速建立,一句頂上面的6句代碼    _imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"HD.jpg"]];                // 1,執行個體化UIScrollView    _scrollView = [[UIScrollView alloc]init];    // 2,設定scrollView的可視大小,內容大小,等屬性    // _scrollView.frame = CGRectMake(0, 0, 320, 480);    _scrollView.frame = self.view.bounds;    _scrollView.contentSize = _imgView.image.size; // 這個最重要,是捲動區域    _scrollView.showsHorizontalScrollIndicator = NO;    _scrollView.showsVerticalScrollIndicator = NO;    _scrollView.bouncesZoom = NO;    _scrollView.bounces = YES;    // scrollView.contentOffset是個點,x(左)和y(上),值是scrollView裡面的大圖片拖拽的位移,相對於scrollView的顯示視窗的最左上方        // 上左下右 逆時針 到邊界之後,回不去了,多出來的距離    // scrollView.contentInset = UIEdgeInsetsMake(5, 10, 20, 40);    // 3,將imgeView添加到scrollView    [_scrollView addSubview:_imgView];    // 4,將scrollView添加到self.view    // [self.view addSubview:_scrollView];    // 小bug解決,介面上的按鈕被完全遮擋了    [self.view insertSubview:_scrollView atIndex:0];                /*      4.scrollView實現縮放(捏合手勢)四步曲            1,設定代理 為 當前控制器            2,代理 尊守 協議 <UIScrollViewDelegate>            3,代理 實現 協議中的方法 viewForZoomingInScrollView,告訴scrollView要縮放的是它內部的哪一個控制項            4,scrollView設定最大最小縮放比 即可     */    _scrollView.delegate = self;    _scrollView.minimumZoomScale = 0.3;    _scrollView.maximumZoomScale = 2.0;                        }// 代理 實現 協議中的方法,告訴scrollView要縮放的是它內部的哪一個控制項-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return _imgView;}- (IBAction)upBtnClick:(id)sender {    // scrollView.contentOffset是個點,x(左)和y(上),值是scrollView裡面的大圖片拖拽的位移,相對於scrollView的顯示視窗的最左上方    [UIView animateWithDuration:0.3 animations:^{        // 以下三步為OC標準代碼,因為OC中不允許直接修該對象中結構體屬性的成員的值,要通過中間的臨時結構體變數        CGPoint offset = _scrollView.contentOffset;        if (offset.y + 80 >= _scrollView.contentSize.height - _scrollView.frame.size.height) {            return;        }        offset.y += 80;        _scrollView.contentOffset = offset;    }];}@end

scrollView的contentOffset



scrollView原理是通過拖拽其內部的大圖片




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.