IOS 基礎開發之 UIScrollView的使用

來源:互聯網
上載者:User

UIScrollView 是我們在項目開發過程中,經常會用到的控制項。

1、初始化

    //init     UIScrollView *scrollView = [[UIScrollView alloc] init];    [scrollView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    [self.view addSubview:scrollView];    [scrollView release];

這裡還可以更加需要設定delegate設定為self。

2、常用屬性

    scrollView.pagingEnabled = YES;    scrollView.showsHorizontalScrollIndicator = YES;    scrollView.showsVerticalScrollIndicator = YES;     scrollView.zoomScale = 2.0f;     scrollView.pagingEnabled = YES;    scrollView.contentOffset = CGPointMake(0, 0);     scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);

。。。。

3、結合 UIPageControl 做頁面滾動效果

首先應該設定如下:

    scrollView.pagingEnabled = YES;    scrollView.delegate = self;

在.h檔案中

@interface scrollViewViewController : UIViewController<UIScrollViewDelegate>{    NSTimer     *myTimer;}@property (retain, nonatomic)  UIPageControl *myPageControl;@property (retain, nonatomic)  UIScrollView *adsScrollView;@end

在.m檔案中

#import "scrollViewViewController.h"@interface scrollViewViewController ()@end@implementation scrollViewViewController@synthesize myPageControl,adsScrollView;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];    // Do any additional setup after loading the view from its nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)initMyScrollView{    //init     UIScrollView *scrollView = [[UIScrollView alloc] init];    [scrollView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    [self.view addSubview:scrollView];    [scrollView release];    //add label    UILabel *showTxt = [[UILabel alloc] init];    [showTxt setFrame:CGRectMake(20, 20, scrollView.frame.size.width - 40, scrollView.frame.size.height - 40)];    showTxt.backgroundColor = [UIColor clearColor];    showTxt.text = @"add label";    [scrollView addSubview:showTxt];    [showTxt release];        scrollView.pagingEnabled = YES;    scrollView.delegate = self;    myPageControl = [[UIPageControl alloc] init];    [myPageControl setFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height)];    [myPageControl addTarget:self action:@selector(pageValueChanged:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:myPageControl];}-(void)scrollToNextPage:(id)sender{    int pageNum = myPageControl.currentPage;    if (pageNum == 4) {        pageNum = -1;    }    [adsScrollView setContentOffset:CGPointMake(adsScrollView.frame.size.width * (pageNum + 1), 0) animated:YES];}- (void)pageValueChanged:(id)sender {    [adsScrollView setContentOffset:CGPointMake(adsScrollView.frame.size.width * myPageControl.currentPage, 0) animated:YES];}#pragma mark - UIScrollViewDelegate-(void)scrollViewDidScroll:(UIScrollView *)scrollView{        CGFloat pageWidth=self.myPageControl.frame.size.width;    int currentPage = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;    self.myPageControl.currentPage = currentPage;}-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];    }-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    [myTimer invalidate];}-(void)dealloc{    [super dealloc];    [myPageControl release];    [adsScrollView release];}@end

4、圖片的縮放

首先,預定義:

#define ZOOM_VIEW_TAG 200#define ZOOM_STEP 2.0

初始化如下:

[scenicImgView setTag:ZOOM_VIEW_TAG];    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];    UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];        [doubleTap setNumberOfTapsRequired:2];    [twoFingerTap setNumberOfTouchesRequired:2];        [scenicImgView addGestureRecognizer:singleTap];    [scenicImgView addGestureRecognizer:doubleTap];    [scenicImgView addGestureRecognizer:twoFingerTap];        [singleTap release];    [doubleTap release];    [twoFingerTap release];        float minimumScale = [myScrollView frame].size.width  / [scenicImgView frame].size.width;    [myScrollView setMinimumZoomScale:minimumScale];    [myScrollView setMaximumZoomScale:1.0];    [myScrollView setZoomScale:minimumScale];

手勢、回調方法如下:

#pragma mark UIScrollViewDelegate methods- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {    return [myScrollView viewWithTag:ZOOM_VIEW_TAG];}- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {    [scrollView setZoomScale:scale+0.01 animated:NO];    [scrollView setZoomScale:scale animated:NO];}#pragma mark TapDetectingImageViewDelegate methods- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {    // single tap does nothing for now}- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {    // double tap zooms in    float newScale = [myScrollView zoomScale] * ZOOM_STEP;    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];    [myScrollView zoomToRect:zoomRect animated:YES];}- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {    // two-finger tap zooms out    float newScale = [myScrollView zoomScale] / ZOOM_STEP;    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];    [myScrollView zoomToRect:zoomRect animated:YES];}#pragma mark Utility methods- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {    CGRect zoomRect;        zoomRect.size.height = [myScrollView frame].size.height / scale;    zoomRect.size.width  = [myScrollView frame].size.width  / scale;        // choose an origin so as to get the right center.    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);        return zoomRect;}

參考:http://www.2cto.com/kf/201111/112631.html

相關文章

聯繫我們

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