As many others, i am trying to implement a zooming behavior with the UIScrollView. I am using Auto Layout to layout my subviews.
In IB i have:
ScrollView
ImageView
The imageView has constraints to fill up the entire scrollView, so the image will be displayed in it's full size on load. This works like a charm. However, i cannot, for the life of me, figure out how to enable zooming. So far i have this code:
[_imgViewInScroll setImage:trackImg];
_scrImageViewer.scrollEnabled = YES;
_scrImageViewer.delegate = self;
_scrImageViewer.contentSize = trackImg.size;
_scrImageViewer.minimumZoomScale = 1;
_scrImageViewer.maximumZoomScale = 4;
And the implemented protocol:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog(#"Zooming");
return imgView;
}
But it doesn't work. The image is displayed, in full size filling the entire screen on load as expected, but it just won't respond to zooming at all. The NSLog statement gets executed, so it takes in the zooming request, but doesn't zoom.
What can i do? Thanks in advance.
I think you are returning the wrong imageview name. Replace "imgView" by "_imgViewInScroll".
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog(#"Zooming");
return _imgViewInScroll;
}
Related
I intend to create a photo viewer.I have a scrollView which have 3 pages. The second page contains an ImageView . I want to return the imageView as the zoomable view.
But every time I start to zoom, it automatically scroll to the first page while the imageView did zoomed ( not on screen, can not scroll). I don’t know what is wrong.
I write some code in the delegate method :-(void)didzoomand method like [scrollview setContentOffset].
I am very confused now.
You need to set ZoomScale to you ScroolView then set delegate.
scrollview.maximumZoomScale = 3.0;
scrollview.delegate = self;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imageView;
}
Am using scrollView to implement Gallery like View. But i am stuck. I have dynamic, say 10 pics to be shown in the horizontal scroller, once at a time. am using following code
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
UIImageView *img1=[array_for_ImageView objectAtIndex:currentImageView];
return img1;
}
problem is, it works fine for the first ImageView. But creates a huge rendering problem when trying to zoom the image at 2, 3 etc vertex. I want to implement scrolling and zooming both.
I also used
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
to check when the zooming end so that I can reset UIscrollView size again.
Please guide me.
I have a screen which has two UIScrollViews. Inside the scrollview is a zoomable UIImageView.
I'd like to achieve the same scrolling and zooming inside one scrollView to be applied to the other one. i.e. if the user pans across the image, both scrollviews pan their images at the exact same rate. If the user pinch zooms the image in one, the other zooms exactly the same amount.
I've read on here about using the zoomToRect:animated: call. I'm not sure exactly how to implement that, so I've tried the following - but it doesn't seem to yield the right results. NB. the scrollView contain self.imageViewLeft. self.scrollViewRight is the scrollView not being touched.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:self.imageViewLeft];
[self.scrollViewRight zoomToRect:visibleRect animated:false];
}
I solved it guys! Props to me.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.scrollViewLeft) {
[self.scrollViewRight setZoomScale:[scrollView zoomScale]];
[self.scrollViewRight setContentOffset:[scrollView contentOffset]];
} else {
[self.scrollViewLeft setZoomScale:[scrollView zoomScale]];
[self.scrollViewLeft setContentOffset:[scrollView contentOffset]];
}
}
I am working with aUIScrollview, and based on if the user scrolls left or right, I reconfigure the UI. The problem is that I need to verify that the user definitely crossed from one screen to another (something along contentOffset).
I've tried using this method:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
But this will fire if the user kind of moves the scrollview partially in one direction, but then doesn't complete the gesture.
I've also tried this method:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
but mainly the same issue; the scrollview scrolls left and right, but on an iPhone, with a content with of 640 ( 320 * 2). I am trying to figure out if the scroll did cross over or not, from one location to the other.
Any suggestions?
I am not entirely sure what you mean by "crossed from one screen to another" I assume you mean paging? If so, here's what you can do:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
int page = sender.contentOffset.x / width;
[self.pageControl setCurrentPage:page]; // in case you need it for updating a UIPageControl
}
I found a way of doing this :
I have a variable to maintain the x location of the contentOffset
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
currX = self.scrollView.contentOffset.x;
}
And then in this event :
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if(self.scrollView.contentOffset.x == currX ) {
return;//This is what I needed, If I haven't panned to another view return
}
//Here I do my thing.
I'm trying to make a basic proof of concept app. It uses UIScrollView with an UIImageView inside of it.
I set the image then set the contentSize of the UIScrollView but I still can't seem to scroll, it always bounces back to the start. Does anyone have any idea what I might be missing?
PS. Zooming works fine.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.picture setImage:nil];
[self.picture setImage:self.image];
self.scroll.contentSize = self.picture.frame.size;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.picture;
}
Hi everyone ok, I've figured out what the problem was other then the fix below. It works as soon as i turn off the Autolayout option in storyboard.
When you set the image on a UIImageView, the frame of it does not update to the size of the new image. What you'll want to do is call the following to update the frame frame of picture to match the size of the image.
[self.picture sizeToFit];
On a side note, you dont need to set the image on nil on the imageView before changing it.