Current location - Health Preservation Learning Network - Healthy weight loss - How does IOS use UIScrollView to scroll and zoom his content?
How does IOS use UIScrollView to scroll and zoom his content?
UIScrollView is one of the most useful controls in IOS. This is a good way to display multiple screens. There are many techniques you can use him below.

This article is about UIScrollView. In short, let's see what we will learn next:

1: How to display a larger picture using UIScrollView?

2. How to stay in the middle when 2.UIScrollView zooms?

3. How to embed complex view hierarchy in UIScrollView?

4.4: How can the features of UIScrollView browse the contents of multiple pages together with UIPageControl?

5. Create a UIScrollView scrolling view, on which you can see the next page and part of the previous page, as well as the current page. This is like the effect of a browsing application in the appstore.

This article is the environment of xcode4.5 above IOS5.0.

We started to create the following projects:

picture

We fill in the name of the project, the company logo you wrote when you created appid, and the prefix of the class name, and set our device as iPhone. For the time being, we only support iPhone mode and choose single view template. Select Next, and then select the save location.

Because we introduced four effects of UIScrollView, we created a tableView, in which a new view controller appeared in each cell and an effect was displayed.

Image (1)

The picture above shows what your storyboard looks like when it is finished now.

We compile the navigation of UITableView, and the next thing to do is:

1: Open MainStroyboard.storyboard and click the system template to create the first initialization scene for us.

2. Then we add a UITableViewController from the object library and put it on the storyboard.

3. Now select what tableView just added, then select Editor, and then select Embedded in, NavigationController.

4. Select tableView of tableViewController and set its cell type as Static Property Checker.

5. Finally, set the part of tableView as a part containing four cells, and set the cells as basic types. Then change their labels to image scrolling, custom view scrolling, paging with browsing.

Image (2)

Save this storyboard and compile it. You should be able to see your desktop view. As shown in the figure below:

Image (3)

Scroll to zoom a large picture:

What we need to do next is to learn how to zoom and scroll large pictures with UIScrollView.

The first step is to set this Viewcontroller, select ViewController.h, and add an exit of UIScrollView. Make your controller conform to the UIScrollView delegation agreement, as shown below.

Copy code

# Import & ltuikit/uikit.h >

@ interface view controller:uiview controller & lt; UIScrollViewDelegate & gt

@property (nonatomic,strong)IBOutlet ui scroll view * scroll view;

@end

Copy code

Then set the implementation properties in ViewController.m

@ synthese scroll view = _ scroll view;

Go back to the storyboard, drag and drop a Viewcontroller from the object library, and set his class as Viewcontroller.

Image (4)

Click Cell CRL of tableview, click the left mouse button and drag it to a new Viewcontroller. A storyboard fragment will pop up and select the push effect.

Drag a UIScrollView from the object library onto the Viewcontroller and populate it.

Image (5)

Then connect the output port of UIScrollView and set Viewcontroller as the proxy of UIScrollView. As shown in the figure below:

Image (6)

Now add properties and methods to the extension in Viewcontroller.m These property methods are private.

Then add

@ synthesize imageView = _ imageView

Now let's set up our UIScrollView in viewDidLoad and viewVillAppear.

Use the following code:

Copy code

- (void)viewDidLoad {

[super viewdidload];

// 1

ui image * image =[ui image image named:@ " photo 1 . png "];

self . imageview =[[ui imageview alloc]init with image:image];

self.imageView.frame = (CGRect){。 origin=CGPointMake(0.0f,0.0f),。 size = image . size };

[self . scroll view add subview:self . imageview];

// 2

self . scroll view . content size = image . size;

// 3

UITapGestureRecognizer * doubleTapRecognizer =[[UITapGestureRecognizer alloc]initWithTarget:self action:@ selector(scrollViewDoubleTapped:)];

doubletaprecognizer . numberoftapsrequired = 2;

doubletaprecognizer . numberoftouchesrequired = 1;

[self . scroll view addGestureRecognizer:doubleTapRecognizer];

UITapGestureRecognizer * twfingertaprecognizer =[[UITapGestureRecognizer alloc]initWithTarget:self action:@ selector(scrollviewtwfingertapped:)];

twfingertaprecognizer . numberoftapsrequired = 1;

twfingertaprecognizer . numberoftouchesrequired = 2;

[self . scroll view addGestureRecognizer:two fingertaprecognizer];

}

-(void)viewWillAppear:(BOOL: (bool) animation {

[Super Vision Will Appearance: Animation];

// 4

CG rect scrollViewFrame = self . scrollview . frame;

CG float scale width = scrollviewframe . size . width/self . scroll view . contentsize . width;

CG float scale height = scrollviewframe . size . height/self . scroll view . contentsize . height;

CGFloat minScale = MIN(scaleWidth,scale height);

self . scroll view . minimumzoomscale = min scale;

// 5

self . scroll view . maximumzoomscale = 1.0f;

self . scroll view . zoom scale = min scale;

// 6

[self centerScrollViewContents];

}

Copy code

The code above looks a bit complicated. So we stopped to analyze it step by step.

1: firstly, you need to create a UIImageView, set its Image attribute, and then set the coordinates of UIImageView and add them to UIScrollView.

2. Then we set the contentSize of UIScrollView to let UIScrollView know how far or how many pixels it can scroll horizontally and vertically.

3. Then two gestures are added to UIScrollView: one is to zoom out by double-clicking gesture, and the other is to zoom in by double-clicking gesture.

4. Next, we need to calculate the minimum size of UIScrollView. The zoom ratio is 1, which means that the contents of UIScrollView are displayed in a normal size. Less than 1, the display content is enlarged, when

Greater than 1 indicates that the content is reduced. In order to get the minimum zoom ratio, you need to calculate how much you want to zoom according to the width of the picture to make the picture comfortably displayed in UIScrollView. Then you do the same calculation according to his height. Finally, set the smallest of the two scaling factors as the smallest scaling factor of UIScrollView. Give you a scale and then zoom in to see the whole picture.

5: You set the maximum zoom ratio to 1, because the zoom is greater than the resolution of the picture, and you will see that the picture is blurred. You set the initial scale to the minimum scale. So this picture can be fully enlarged.

6: Always keep the picture in the middle of UIScrollView when zooming.

Copy code

-(void)centercrollviewcontents {

CGSize boundsize = self . scroll view . bounds . size;

CG rect contents frame = self . imageview . frame;

if(contents frame . size . width & lt; boundsSize.width) {

contents frame . origin . x =(bounds size . width-contents frame . size . width)/2.0f;

} Otherwise {

contents frame . origin . x = 0.0f;

}

if(contents frame . size . height & lt; boundsSize.height) {

contents frame . origin . y =(bounds size . height-contents frame . size . height)/2.0f;

} Otherwise {

contents frame . origin . y = 0.0f;

}

self . imageview . frame = contents frame;

} If the boundsize of UIScrollView is greater than the frame size of UIImageView picture, then the coordinates of the picture are the calculated results when the condition is true, and vice versa. -(void) scrollviewdoubletapped: (uitapgesturecognizer *) recognizer {

// 1

CG point point inview =[recognizer location inview:self . imageview];

// 2

CG float newZoomScale = self . scroll view . zoom scale * 1.5f;

newZoomScale = MIN(newZoomScale,self . scroll view . maximumzoomscale);

// 3

CGSize scrollViewSize = self . scrollview . bounds . size;

CG float w = scrollviewsize . width/newZoomScale;

CG float h = scrollviewsize . height/newZoomScale;

CG float x = point inview . x-(w/2.0f);

CG float y = pointin view . y-(h/2.0f);

CGRect rectToZoomTo = CGRectMake(x,y,w,h);

// 4

【self . scroll view zoom torct:rectto zoom to animated:YES】;

}

Copy code

1: Get the coordinate position of the picture you clicked.

2. Next, calculate the scaling ratio of 150%, but the maximum scaling ratio must be limited.

3: Then use the position calculated in the first step to calculate the size of the position you want to zoom.

4. Finally, you need to tell UIScrollView to view the scaled frame and add animation.

-(void)scrollviewtwfingertapped:(UITapGestureRecognizer *)recognizer {

//Zoom out slightly, and cap with the minimum zoom ratio specified by scrolling view.

CG float newZoomScale = self . scroll view . zoom scale/ 1.5f;

newZoomScale = MAX(newZoomScale,self . scroll view . minimumzoomscale);

【self . scroll view setZoomScale:newZoomScale animated:YES】;

}

This is similar to the way to enlarge.

-(ui view *)viewforzoominginscolllview:(ui scroll view *)scroll view {

//Return the view to zoom.

Return to self.imageView

}

This is the soul of UIScrollView zoom mechanism. When UIScrollView finishes zooming, you tell him which view is zoomed in UIScrollView.

-(void)scroll viewdidzoom:(ui scroll view *)scroll view {

//The scrolling view has been scaled, so you need to re-center the content.

[self centerScrollViewContents];

}

This method is that when UIScrollView finishes zooming, it needs to inform that the view is in the middle of UIScrollView, otherwise UIScrollView will not naturally zoom.

Picture (7)

Compiling and running the project has the above effect, so you can try to zoom in and out.