iOS - UIScrollView can scroll but cannot zoom - ios

I want to zoom in/out UIScrollView that contains a UIImageView. With my code below I am only able to scroll the scroll view contents but cannot zoom it.
My view hierarchy looks like this:
- (UIView *) containerView
-- (UIView *) contentView
--- (UIScrollView *) scrollView
---- (UIImageView *) self.imageView
Code:
UIView *previousContentView = nil;
for (NSInteger i = 0; i < 2; i++) {
contentView = [self addRandomColoredView];
[containerView addSubview:contentView];
[containerView.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
[containerView.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = true;
scrollView = [self addRandomScrollView];
[contentView addSubview:scrollView];
self.imageView = [[UIImageView alloc] init];
[self.imageView setImage:[imagesArray objectAtIndex:i]];
[self.imageView setTranslatesAutoresizingMaskIntoConstraints:false];
[scrollView addSubview:self.imageView];
if (previousContentView) {
[VerticalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
NSLayoutConstraint *width = [contentView.widthAnchor constraintEqualToAnchor:previousContentView.widthAnchor];
width.priority = 250;
width.active = true;
} else {
[containerView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
}
[contentView.topAnchor constraintEqualToAnchor:scrollView.topAnchor].active = true;
[contentView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor].active = true;
[contentView.leadingAnchor constraintEqualToAnchor:scrollView.leadingAnchor].active = true;
[contentView.trailingAnchor constraintEqualToAnchor:scrollView.trailingAnchor].active = true;
[scrollView.topAnchor constraintEqualToAnchor:self.imageView.topAnchor].active = true;
[scrollView.bottomAnchor constraintEqualToAnchor:self.imageView.bottomAnchor].active = true;
[scrollView.leadingAnchor constraintEqualToAnchor:self.imageView.leadingAnchor].active = true;
[scrollView.trailingAnchor constraintEqualToAnchor:self.imageView.trailingAnchor].active = true;
previousContentView = contentView;
}
[containerView.trailingAnchor constraintEqualToAnchor:previousContentView.trailingAnchor].active = true;
- (UIView *)addRandomColoredView
{
UIView *someView = [[UIView alloc] init];
someView.translatesAutoresizingMaskIntoConstraints = false;
[someView setBackgroundColor:[UIColor blackColor]];
return someView;
}
-(UIScrollView *)addRandomScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
[scrollView setDelegate:self];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:false];
[scrollView setBackgroundColor:[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]];
[scrollView setMaximumZoomScale:2.5f];
[scrollView setMinimumZoomScale:0.5f];
[scrollView setZoomScale:scrollView.minimumZoomScale];
return scrollView;
}
#pragma mark - UIScrollViewDelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
I believe the scrollView is getting its contentSize because it is showing the image and I can scroll. Why can't I zoom in or zoom out?

You have to specify a delegate for your scroll view, and then implement viewForZoomingInScrollView to tell it to zoom the image view:
- (UIView *)addScrollViewWithImageView {
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = false;
scrollView.maximumZoomScale = 200.0;
scrollView.minimumZoomScale = 0.1;
scrollView.delegate = self;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
imageView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:scrollView];
[scrollView addSubview:imageView];
[NSLayoutConstraint activateConstraints:#[
[imageView.topAnchor constraintEqualToAnchor:scrollView.topAnchor],
[imageView.leftAnchor constraintEqualToAnchor:scrollView.leftAnchor],
[imageView.rightAnchor constraintEqualToAnchor:scrollView.rightAnchor],
[imageView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor]
]];
return scrollView;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return scrollView.subviews.firstObject;
}
And then to populate these zoomable scroll views with image views, your viewDidLoad might look like:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *previousContentView = nil;
for (NSInteger i = 0; i < 3; i++) {
UIView *contentView = [self addScrollViewWithImageView];
[self.view.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
[self.view.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor].active = true;
if (previousContentView) {
[HorizontalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
NSLayoutConstraint *height = [contentView.heightAnchor constraintEqualToAnchor:previousContentView.heightAnchor];
height.priority = 250;
height.active = true;
} else {
[self.view.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
}
previousContentView = contentView;
}
[self.view.bottomAnchor constraintEqualToAnchor:previousContentView.bottomAnchor].active = true;
}

Try MWPhotoBrowser. It has built in scroll and zoom functionality. You will get more than you need. Good Luck.

Related

Image not Zoom On double tap in iOS

In my application having gallery functionality.For Scrolling image i used REPagedScrollView and i want to image zoom on pinch using 2 finger and also want zoom on double tap but i unable to to zoom image on double tap
please resolved my problem.
I tried by below code :
.h file
#interface ViewPhotoVC : UIViewController<UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
REPagedScrollView *scrollView;
UIScrollView *image_scroll;
int current_page;
BOOL is_FullScreen;
}
#property (strong,nonatomic) IBOutlet UIView *slider_view;
.m file
-(void)video_image_Gallery
{
scrollView = [[REPagedScrollView alloc] initWithFrame:self.slider_view.bounds];
scrollView.delegate=self;
// scrollView.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
// scrollView.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
current_page=0;
for(int i=0;i<[self.PhotoImgArr count];i++){
self.image_view = [[AsyncImageView alloc] initWithFrame:CGRectMake(0,0, self.slider_view.frame.size.width, self.slider_view.frame.size.height)];
// scrollView.frame=CGRectMake(0, 0, screenwidth, screenheight);
image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenwidth,screenheight)];
self.image_view.backgroundColor=[UIColor clearColor];
self.image_view.contentMode = UIViewContentModeScaleAspectFit;
self.image_view.clipsToBounds = true;
self.image_view.userInteractionEnabled = YES;
self.image_view.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
self.image_view.image = [UIImage imageNamed:#"no_image1.png"];
self.image_view.imageURL =[NSURL URLWithString:[self.PhotoImgArr objectAtIndex:i]];
self.image_view.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:246.0/255.0 blue:241.0/255.0 alpha:1];
[image_scroll setDelegate:self];
[image_scroll setShowsHorizontalScrollIndicator:NO];
[image_scroll setShowsVerticalScrollIndicator:NO];
[image_scroll setMaximumZoomScale:8.0];
image_scroll.tag=i+1;
self.PlayVideoBtn.tag=i+1;
self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
image_scroll.contentSize = CGSizeMake(self.image_view.bounds.size.width-500, self.image_view.bounds.size.height-300);
image_scroll.decelerationRate = UIScrollViewDecelerationRateFast;
// [image_scroll setMinimumZoomScale:[image_scroll frame].size.width / [self.image_view frame].size.width];
[image_scroll setZoomScale:[image_scroll minimumZoomScale]];
[image_scroll addSubview:self.image_view];
[image_scroll addSubview:self.PlayVideoBtn];
[image_scroll addSubview:self.videoImg];
scrollView.tag=0;
// self.PlayVideoBtn.tag=0;
[scrollView addPage:image_scroll];
}
[self.slider_view addSubview:scrollView];
self.index_lbl.text = [NSString stringWithFormat:#"%d of %lu",(int)self.selected_index+1,(unsigned long)[self.PhotoImgArr count]];
[scrollView scrollToPageWithIndex:self.selected_index animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
if(scrollView1.tag ==0){
CGFloat pageWidth = scrollView1.frame.size.width;
current_page = floor((scrollView1.contentOffset.x - pageWidth / 2.0) / pageWidth) + 1;
self.index_lbl.text = [NSString stringWithFormat:#"%d of %lu",current_page+1,(unsigned long)[self.PhotoImgArr count]];
if(current_page+1==self.PhotoImgArr.count)
{
self.nextBtn.userInteractionEnabled=NO;
self.nextImg.image=[UIImage imageNamed:#"disable_next.png"];
}
else if(current_page==0)
{
self.prevBtn.userInteractionEnabled=NO;
self.prevImg.image=[UIImage imageNamed:#"disable_prev.png"];
}
else
{
self.nextBtn.userInteractionEnabled=YES;
self.prevBtn.userInteractionEnabled=YES;
self.nextImg.image=[UIImage imageNamed:#"next.png"];
self.prevImg.image=[UIImage imageNamed:#"prev.png"];
}
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1 {
if([self.PhotoOrVideo isEqualToString:#"Photo"])
{
if([[scrollView1 viewWithTag:scrollView1.tag].subviews count]>0){
return [[scrollView1 viewWithTag:scrollView1.tag].subviews objectAtIndex:0];
}
}
return nil;
}
Please try the below code:
-(void)setScrollView:(UIScrollView *)scrollView
{
_scrollView=scrollView;
_scrollView.minimumZoomScale=0.2;
_scrollView.maximumZoomScale=3.0;
_scrollView.delegate=self;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[self.scrollView flashScrollIndicators];
}

Why is the UIImageView not hiding when using a UISegmentedControl?

I have a UISegmentedControl where the first control will display the text within a UITextView while the second control displays a scrollable UIImageView.
In the initial startup, if I switch to the second control, the image displays and switching back to the first control, the image disappears and the UITextView shows.
However, when I switch to the second control the 2nd time and switch back to the first control, the image is still there and I cannot get the UITextView to show anymore.
My code has it to where the image is hidden and the text is shown in the first control, and vice versa in the second control.
Why was it working the first time, but then not working the second time I switched between controls?
What am I doing wrong?
Thanks
-(void)viewDidLoad
{
self.scrollView.delegate = self;
self.textView.text = #"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
}
-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.scrollView addSubview:self.imageView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display apppropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
[self setScroller];
}
}
You should remove [self setScroller]; from the - (IBAction)segmentedControl:(UISegmentedControl *)sender method, and put it in -(void)viewDidLoad instead. You're calling [self setScroller]; every time you switch to the second segment.
Your code should look like:
-(void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self setupScroller];
}
-(void)setupScroller
{
// Set contentSize
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = scrollableSize;
// Add textView
self.textView.text = #"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
// Add ImageView
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.imageView.hidden = YES;
[self.scrollView addSubview:self.imageView];
// Configure Zoom Scales and backgroundColor
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display appropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
}
}
Your problem probably is that you creating a lot of instances of "imageView"
To solve your problem I suggest:
-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
if(self.imageView == nil) {
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
[self.scrollView addSubview:self.imageView];
}
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.imageView setNeedsDisplay];
[self.imageView setImage:[UIImage imageNamed: #"test.png"]];
}
I've put only the Alloc of the self.imageView + the addSubView to the if,
All the rest are out side,
Now it will work

UIScrollview does not react to any touch?

I am trying to do a simple scroll, but the views do not move after a touch, I am not sure why, the scrollview should handle the gesture, but something might be missing. Would someone know where?
Here is the code : I create a small horizontal scroll view, with some views inside. The views appear well, I am testing it on a device for the touch :
- (void)viewDidLoad {
[super viewDidLoad];
//horizontal scroll view
HorizScroll *ho = [[HorizScroll alloc] initWithFrame:CGRectMake(0, 0, 500, 100)];
for ( int i=0; i<3; i++){
MyView* mv = [[MyView alloc] init];
[ho addSubview:mv];
}
//ho.zoomScale = 0.3f;
[self.view addSubview:ho];
}
#implementation HorizScroll
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
-(void)addSubview:(UIView *)view{
[super addSubview:view];
NSUInteger numSubviews = self.subviews.count;
[view setFrame:CGRectMake(CGRectGetWidth(view.bounds)*(numSubviews),
0,
CGRectGetWidth(view.bounds),
CGRectGetHeight(view.bounds) )];
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds) )];
}
#implementation MyView
-(int)getRandomNumberBetween:(int)from to:(int)pto {
return (int)(from + arc4random() % (pto-from+1));
}
-(instancetype)init{
if ( self = [super init] ){
CGFloat red = [self getRandomNumberBetween:1 to:255];
self.backgroundColor = [UIColor colorWithRed:red/256.0
green:[self getRandomNumberBetween:1 to:255]/256.0
blue:[self getRandomNumberBetween:1 to:255]/256.0
alpha:1.0];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if ( self = [super initWithFrame:frame] ){
self.frame = CGRectMake(counterX, 0, 100, 100);
counterX += 50;
}
return self;
}
You need to set contentSize of scrollview to be larger than its frame size. so add this line after [self.view addSubview:ho].
ho.contentSize = CGSizeMake(501.f, 100.f);
or before [self.view addSubview:ho] and comment out the line:
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds))];
which is not necessary since you can set it after all subviews are added.

How to pinch zoom multiple UIImageViews inside UIScrollView

I have to pinch zoom multiple images. I have added each of the UIImageView to UIView and added the UIView to UIScrollView. and returning the UIView image viewForZoomingInScrollView: delegate method, but images are not zooming as expected. is there any better way?
#define VIEW_FOR_ZOOM_TAG (1)
#implementation [SVViewController][1]
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
CGRect innerScrollFrame = mainScrollView.bounds;
for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"page%d", i + 1]]];
imageForZooming.tag = VIEW_FOR_ZOOM_TAG;
UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 2.0f;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];
[mainScrollView addSubview:pageScrollView];
if (i < 2) {
innerScrollFrame.origin.x += innerScrollFrame.size.width;
}
}
mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x +
innerScrollFrame.size.width, mainScrollView.bounds.size.height);
[self.view addSubview:mainScrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
#end
[1]: http://www.lyricspoints.com
Check the minimum and maximum zoom scale properties of the scroll view. (max should be greater than min for zoom to happen)
make sure that you are returning the corresponding UIImageView and not the UIView
Make sure that you have set the scroll view delegate as the object to which you want the delegate messages of the scroll view to be sent, like viewForZoomingInScrollView:

Enable Paging in UIScrollView after Zooming

I have a UIScrollView that loads three different pages.When i zoom in on a page, and zoom back out to the original size, the application stops letting me scroll between the pages, as if paging is disabled. What can i do to re-enable paging when zoomed out to the original size (Scale == 1)?
This is my code
- (void)viewDidLoad
{
[ScView setMaximumZoomScale : 2.0f];
[ScView setMinimumZoomScale : 1.0f];
ScView.contentSize = CGSizeMake(1024*3, 1.0);
ScView.pagingEnabled = YES;
ScView.clipsToBounds = YES;
ScView.delegate = self;
ScView.showsHorizontalScrollIndicator = NO;
ScView.showsVerticalScrollIndicator = NO;
[super viewDidLoad];
[self returnImages];
}
-(void)returnImages{
for (pageNumber = 1; pageNumber <= 3; pageNumber++) {
imagen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",pageNumber]]];
imagen.frame = CGRectMake((pageNumber-1)*1024, 0, 1024, 768);
[ScView addSubview:imagen];
}
}
//
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return ScView;
// return [imagen initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",pageNumber]]];
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view
{
NSLog(#"Scroll Will Begin");
ScView.scrollEnabled = YES;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale
{
if(scale == 1)
{
ScView.scrollEnabled = YES;
ScView.pagingEnabled = YES;
[self returnImages];
NSLog(#"Scrolol will end");
//ScView.maximumZoomScale = 2.0f;
// [super viewDidLoad];
[self returnImages];
}
}
Any ideas will be highly appreciated..
To get proper paging and zooming you have to embed UIScrollView for each page into your parent UIScrollView. This combination will allow you to use simultaneously paging and internal scrolling.
Here is the example of UIViewController with parent scroll view and three embedded zoomable pages.
#define VIEW_FOR_ZOOM_TAG (1)
#implementation SVViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
CGRect innerScrollFrame = mainScrollView.bounds;
for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"page%d", i + 1]]];
imageForZooming.tag = VIEW_FOR_ZOOM_TAG;
UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 2.0f;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];
[mainScrollView addSubview:pageScrollView];
if (i < 2) {
innerScrollFrame.origin.x += innerScrollFrame.size.width;
}
}
mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);
[self.view addSubview:mainScrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
#end
I follow #NikNarmo 's solution, write a small swift xcode project to demo the zooming and paging function.
Hope to help anyone who want to do the same task.
Some code is from UIScrollView Tutorial: Getting Started http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift,
and some from A Beginner’s Guide to UIScrollView http://www.appcoda.com/uiscrollview-introduction/.
Using Xcode 7.0 and Swift 2.0
override func viewDidLoad() {
super.viewDidLoad()
mainScrollView = UIScrollView(frame: self.view.bounds)
mainScrollView.pagingEnabled = true
mainScrollView.showsHorizontalScrollIndicator = false
mainScrollView.showsVerticalScrollIndicator = false
pageScrollViews = [UIScrollView?](count: photos.count, repeatedValue: nil)
let innerScrollFrame = mainScrollView.bounds
mainScrollView.contentSize =
CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width,
mainScrollView.bounds.size.height)
mainScrollView.backgroundColor = UIColor.redColor()
mainScrollView.delegate = self
self.view.addSubview(mainScrollView)
configScrollView()
addPageControlOnScrollView()
}
and the magic is in the func scrollViewWillEndDragging, when the contentSize is equal to the mainScrollViewContentSize or not, if it is mainScrollViewController, then do paging, otherwise do nothing.
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let targetOffset = targetContentOffset.memory.x
let zoomRatio = scrollView.contentSize.height / mainScrollViewContentSize.height
if zoomRatio == 1 {
// mainScrollViewController
let mainScrollViewWidthPerPage = mainScrollViewContentSize.width / CGFloat(pageControl.numberOfPages)
let currentPage = targetOffset / (mainScrollViewWidthPerPage * zoomRatio)
pageControl.currentPage = Int(currentPage)
loadVisiblePages()
}
else {
// pageScorllViewController
}
}
And here is the project code https://github.com/Charles-Hsu/ScrollViewDemo

Resources