Use of uiscrollview in basic iOS development

Source: Internet
Author: User

Uiscrollview is a control that we often use during project development.

1. Initialization

    //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];

You can also set delegate to self.

2. Common attributes

    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. Combined with uipagecontrol for Page scrolling

First, you should set it as follows:

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

In the. h file

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

In the. M file

#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. Image Scaling

First, the prefix is as follows:

#define ZOOM_VIEW_TAG 200#define ZOOM_STEP 2.0

The initialization is as follows:

[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];

The gesture and callback methods are as follows:

#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;}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.