Resize content of UIScrollView when zooming - ios

I know there is many topics for this subject but I didn't found a good solution for my problem.
I have a UIScrollView with an image and other components added as subviews.
I just want to know the good way to resize the components into in real time when I zoom.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// Resizing image and other components
}
EDIT : I would like the impression that the components inside the scroll view keep the same size when I zoom. I don't want to they become bigger or smaller during zooming.
EDIT : Here is the hierarchy of my UIViewController
View
ScrollView
Main view
Image view
Drawing view (UIView with components like UILabel, other UIViews, etc...)
Thanks a lot !

I would leave the scale of UIScrollView as it is and reach the goal with the help of subview (adjusting its size, not scrollView's), where I would place all the subviews and the images you work with.
So it would be something like:
yourView.transform = CATransform3DMakeScale(newScale, newScale, 1.0);
where yourView is the main (only) subview of scrollView. Then I would recalculate the size of the yourView with new scale and set it to scrollView's contentSize parameter so that you could actually scroll the enlarged content, not only see it truncated.
The only thing is that in this case you should use the callback other than scrollViewDidZoom: I guess.
P.S. Changing UIView's transform parameter you don't need to bother about subviews' scale: CoreAnimation does everything for you.

Related

Update Subview in a Xib while animating bounds of superview

I am working on animating in a modal-like window, which has been built in a Xib using auto-layout constraints. I am attempting to make it appear to grow from a UITableViewCell on tap. I've noticed that only the superview will animate it's bounds (width and height) using CAKeyframeAnimation(keyPath: "bounds.size"), while none of it's subviews change at all.
I do not want to use CATransform3D as that ends up distorting images and text in the subview.I've also tried layoutIfNeeded() before and after the animation, but it doesn't seem to make a difference on animating the subviews.I also tried snapshotting, but it would not work because the view is not rendered on the screen until the animation begins.
Any thoughts on making this work would be greatly appreciated.

UIView animation clips view bounds. Any way to prevent clipping?

I have a UIView animation that does a vertical flip animation transition from one view to another. The problem is that the view has some overflowed content (achieved by setting clipsToBounds to NO on the view), and during the animation, the overflowed content gets clipped.
Is there any way to prevent CoreAnimation from clipping the views?
Screenshots
Normal view (notice the paperclip and overhanging rope along the top edge of the map):
Animation in flight: (paperclip and rope are clipped)
I'd recommend placing all the views which rotate inside of a transparent view (kind of placeholder for "map" and "clip"), and applying animation to it rather than to your map view.
Try to set placeholder view's size the way its subviews won't overflow, so you can not worry about hacking clipsToBounds.
have you tried: myView.layer.masksToBounds = NO; ?

How does sizeToFit determine appropriate size?

I have an app where the user can add what I call palettes at will. Items (UIViews) can be dragged from one palette to another. I thought I could use the sizeToFit method to automatically resize the palettes when items were added ore removed, but no resizing seems to be happening.
The documentation states that the method resizes to the most appropriate size to fit the subviews. Does that not include UIViews that I add programmatically?
I'd really not have to write my own sizeToFit method if I can get the built-in one to work for me. But it seems to be doing nothing. Here's the code I've tried:
CGPoint oldCenter = self.pieceBeingMoved.center;
CGPoint newCenter = [self.pieceBeingMoved.superview convertPoint:oldCenter toView:destinationView];
[destinationView addSubview:self.pieceBeingMoved];
self.pieceBeingMoved.center = newCenter;
if (destinationView.tag == 20000) {
NSLog(#"DestinationView is a palette, so resize it from %#",NSStringFromCGRect(destinationView.frame));
[destinationView sizeToFit];
NSLog(#"DestinationView resized to %#",NSStringFromCGRect(destinationView.frame));
}
The size is identical before and after the subview is added. The subview being added is the only subview at the time I ran this test, and the size of the subview is about 5% the size of the palette it is being moved to. So why isn't the view being resized? Do I need to do it myself?
The sizeToFit method just calls sizeThatFits: and then resizes the view accordingly. The default implementation of the UIView class returns the current size so you don’t see any effect. You will have to write your own sizeThatFits: method to do your calculation.
Here's an idea. Don't call sizeToFit. Just set the frame to the frame you actually want.

iOS 6 Autolayout - ScrollView with embedded ImageView not zooming properly

I am trying to setup a simple view that displays an image and allows users to zoom in/out and pan the image as they desire.
I have autoLayout enabled and the view consists of a UIScrollView with an embedded UIImageView.
Both views are sized to the entire screen.
I used the logic and code from Apple's ScrollViewSuite (the "1_TapToZoom" sample) and everything seems correct. However, when pinching the view, the ScrollView/ImageView gets all distorted and doesn't respond properly. Hard to explain the behavior, but lets just call it "funky". The size and aspect changes in no consistent manner.
My GUESS is that this has to do with autolayout constraints.
Does anyone have some suggestions/fixes for this?
The issues that occurs is the changing of location of the imageview during the zoom process. The origin location of the imageview will change to be a negative value during the zoom. I believe this is why the jerky movement occurs. As well, after the zoom is complete the imageView is still in the wrong location meaning that scrolls will appear to be offset.
If you implement -(void) scrollViewDidZoom:(UIScrollView *)scrollView and log the frame of the UIImageView during this time you can see its origin changing.
I ended up making things work out by implementing a strategy like this: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch20p573scrollViewAutoLayout/ch20p573scrollViewAutoLayout/ViewController.m
And in addition changing the frame of the contentView while zooming
-(void) scrollViewDidZoom:(UIScrollView *)scrollView {
CGRect cFrame = self.contentView.frame;
cFrame.origin = CGPointZero;
self.contentView.frame = cFrame;
}

Scroll view design feasibility - iOS

Im working on a scroll view design where the 'black boxes are supposed to be image items.The blue box is the scroll view. Is it possible to have a zig zag format? And for the count of the box depends on the images available on the internet. So it is more dynamic. Any suggestions would be greatly appreciated!
Yes, you can definitely do this.
You will be positioning UIImageView objects inside the content area of a UIScrollView. Simply set the contentSize of your UIScrollView to reflect the size of your blue box. Think of UIScrollView as a film strip. The UIScrollView is the visible portion of your film, and the entirety of the film is your contentSize.
Now position each UIImageView by setting its frame as you wish and then add it as a subview to the scrollview. If you find yourself needing more horizontal space, you can resize the contentSize accordingly.

Resources