iOS UIScrollView always bounces back - ios

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.

Related

iOS: Can a UIView know that the layout process for it has completed?

When using auto layout, the view's size is unknown when it is initialised, this brings a problem to me.
When using UIImageView, I wrote a category that can load image from my own CDN by setting the image URL to UIImageView, my CDN stores one image with different sizes so that difference devices can load the size it really needs.
I want to make my UIImageView be able to load the URL for the resolution it needs, but when my UIImageView get the URL, the size of it is not yet determined by auto layout.
So is there a way for UIView to know that the layout process for it has finished for the first time?
there is a method for UIView.You could override it.
-(void)layoutSubviews {
CGRect bounds =self.bounds;
//build your imageView's frame here
self.imageView=imageViewFrame.
}
In Swift 5.X
override func layoutSubviews() {
super.layoutSubviews()
let myFrame = = self.bounds
}
If you have other complex items in the custom view, don't forget to call super if you override layoutSubviews()...
Sadly, there is no straightforward way that a UIView can be notified that its constraints have been set. You can try a bunch of different things though,
Implement layoutSubviews function of a UIView, this is called whenever UIView's layout is changed.
Implement viewDidLayoutSubviews of the UIViewController that has it inside it. This function is called when all the layouts have been set. At this point you can your category function.
here's a test, I only use autolayout and I only use custom subclassed subviews. I do all auto layout in the initializer of the subclass:
This is for a "login button" that has no frame but is then set with autolayout:
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.contentView.loginButton.frame.size.height) {
NSLog(#"viewDidLayoutSubviews");
}
}
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (self.contentView.loginButton.frame.size.height) {
NSLog(#"viewWillLayoutSubviews");
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.contentView.loginButton.frame.size.height) {
NSLog(#"didAppear");
}
}
-(void)viewWillAppear:(BOOL)animated {
if (self.contentView.loginButton.frame.size.height) {
NSLog(#"viewWillAppear");
}
}
-(void)viewDidLoad {
[super viewDidLoad];
if (self.contentView.loginButton.frame.size.height) {
NSLog(#"viewDidLoad");
}
}
Here's the output. So, this means that my view finally has a frame when the
2015-08-25 01:28:27.789 [67502:1183631] viewDidLayoutSubviews
2015-08-25 01:28:27.790 [67502:1183631] viewWillLayoutSubviews
2015-08-25 01:28:27.790 [67502:1183631] viewDidLayoutSubviews
2015-08-25 01:28:28.007 [67502:1183631] didAppear
This means that the first time the login button has a frame is in the viewDidLayoutSubviews, this will look weird becuase this is the first pass of main view's subviews. There's no frame in ViewWillAppear although the view of the viewcontroller itself is already set before the login button. The entire UIView subclass's main view is also set when the viewcontroler's view is set, this happens before the login button as well. So, the subviews of the view are set after the parent view is set.
The point is this: if you plop the imageview information pull in the viewDidLayoutSubviews then you have a frame to work with, unless you set this UIImageView frame to the view of the ViewController by type casting then you will have the UIImageView's frame set in the viewDidLoad. Good luck!

UIScrollView zoom auto

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

not able to Implement Scrollview(scrolling and zooming) for Gallery View

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.

Make 2 UIScrollViews zoom and scroll identically

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

App Store screenshot auto scroll effect

In the Appstore screen shots section, just wondering how would someone implement the way Apple automatically scrolls down right at the screen shot edges when a user starts paging the screenshots horizontally?
Did they use animation? How would you do it?
Thanks!
They probably use the UIScrollView method:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
or the UIScrollView method:
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
When the horizontal scroll view's delegate method
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
gets called, figure out the rect or point you want, and then do:
[theVerticalScrollView scrollRectToVisible:theRectYouWant animated:YES];
or
[theVerticalScrollView setContentOffset:thePointYouWant animated:YES];

Resources