iOS開發-ScrollView圖片縮放

來源:互聯網
上載者:User

標籤:

智能手機一般常用常用的操作觸摸,滑動,縮放,感覺對於生活而言就是手機在手,天下我有,看網頁的時候字型太小,縮放一下,看美女的看的不爽,縮放一下,地圖看的不清,縮放一下。縮放是一個很常見的操作,不論是從生活還是寫程式而言,都是一個繞不開的東西,做了一個Demo,縮放一下美女,熟悉ScrollView中的常見屬性的設定,開始正題吧。

常見屬性

先看圖,要實現的效果:

  

    UIImage *image=[UIImage imageNamed:@"girl0.jpg"];        _imageView=[[UIImageView alloc] initWithImage:image];        [_scrollView addSubview:_imageView];    //設定ScrollView和image是一樣的大小    [_scrollView setContentSize:image.size];

 可以設定ScrollView的初始位置和大小:

    //CGRect枚舉一個矩形,然後設定imageView的位置    [_imageView setFrame:CGRectMake(0, 0, 100, 100)];

設定邊界地區:

    //設定邊界地區   [_scrollView setContentInset:UIEdgeInsetsMake(20, 20.0, 20.0, 20.0)];

 上下左右移動調用哪個同意IBAction,通過Tag區分(之前文章有介紹),移動就是控制座標,IOS中左上方是0,X軸向右自增,Y軸向下自增:

    UIButton *button=(UIButton *)sender;    CGPoint currentPoint=self.scrollView.contentOffset;    switch (button.tag) {        case 0:            currentPoint.y-=50;            break;        case 1:            currentPoint.y+=50;            break;        case 2:            currentPoint.x-=50;            break;        case 3:            currentPoint.x+=50;            break;        default:            break;    }    //橫軸的邊界值    if (currentPoint.x<0) {        currentPoint.x=0;    }else if (currentPoint.x>_scrollView.contentSize.width-_scrollView.bounds.size.width){        currentPoint.x=_scrollView.contentSize.width-_scrollView.bounds.size.width;    }        //縱軸的邊界值    if (currentPoint.y<0) {        currentPoint.y=0;    }else if (currentPoint.y>_scrollView.contentSize.height-_scrollView.bounds.size.height){        currentPoint.y=_scrollView.contentSize.height-_scrollView.bounds.size.height;    }            //動畫效果    [self.scrollView setContentOffset:currentPoint animated:YES];

 動畫效果可以通過block設定:

    [UIView animateWithDuration:0.3f animations:    ^{         [self.scrollView setContentOffset:currentPoint];    }];
縮放

縮放之前需要涉及到一個東西就是控制器需要遵守UIScrollViewDelegate協議,然後實現協議中方法,應用情境中如果我們在對ScrollView中圖片進行縮放,將訊息通知給UIScrollViewDelegate,最終將事件實現委託給是實現方法:

////  ViewController.h//  ScrollView//  http://www.cnblogs.com/xiaofeixiang//  Created by keso on 15/1/20.//  Copyright (c) 2015年 keso. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIScrollViewDelegate>@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@end

設定一下最大和最小縮放比例,設定委託:

    [_scrollView setMinimumZoomScale:0.3];    [_scrollView setMaximumZoomScale:1.8];    [_scrollView setDelegate:self];

實現一個返回的映像,如果不是實現,沒有效果:

//縮放過程中的映像- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return _imageView;}

有的時候如果可能有業務需要會需要一個縮放結束的方法:

////縮放結束- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{    NSLog(@"縮放比例:%f",scale);}

還有一個不常用的,縮放中的方法:

//縮放中- (void)scrollViewDidZoom:(UIScrollView *)scrollView{    NSLog(@"縮放中的調用~");}

 最終效果:

iOS開發-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.