How to pinch to zoom a view without making the view bigger? - ios

This is what I currently have:
func handlePinching(recognizer : UIPinchGestureRecognizer) {
self.layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformScale(self.transform, recognizer.scale, recognizer.scale));
recognizer.scale = 1.0;
}
Using self.view.transform for that also makes it bigger. I want to make it zoom "internally" (I really don't know how to explain it).

So I'm not 100% sure to understand the question but I guess I get it.
For example you want to zoom on an image without zooming the other element of the UI (NavBar, Buttons, ...) right?
So I guess in you're example you're in a viewController, which means, when you change the scale of self.view you'll zoom everything. You have to apply the scale on the specific view that you want to zoom in.
In the example below, to zoom on the image, the image is inside of an UIImageView, and this imageView is subView of self.view. And you will just apply a transform on this imageView.
Moreover I think you get a little bit confused on how to zoom, considering the view you want to zoom is imageView you just need to do
imageView.transform = CGAffineTransformMakeScale(recognizer.scale,recognizer.scale)
I hope this answer your question, let me know if something is not clear.

Related

Zoom in/out of a whole Tableview

Problem:
I got a Tableview with dynamically many cells. The cells contain PKDrawings with the PencilKit Framework. So its like a book...
I would like to do 2 things:
1. Zoom in so i am closer to the cells-content so the drawings seem bigger.
(But when I say cells-content I mean all cells so I am talking about the Tableview. I don't want to zoom into a specific cell)
2. Zoom out so I start seeing more and more pages (cells) above and below the one that was "in my focus" when not zooming out or in.
If that's not possible as the Cells are Hidden and its hard to tell them while zooming out when to load again its fine if the maximal zoom-out is the point when I haven't zoomed in.
As a Tableview is already a subclass of the UIScrollview I can access its min & max Zoomscale. However setting this doesn't change anything. The ScrollViewDidZoom won't get triggered either.
Scrolling works fine though...
I think that this should all be possible if I somehow manage it to put my Tableview into another View/Scrollview and just zoom in/out on that but I don't know how to do that...
Thanks in advance for any help!
For zooming UITableview cell u can use CGAffineTransform. You can manage the scale value as per requirement
// For Zoom in
CGAffineTransform trans = CGAffineTransformScale(cell.contentView.transform, 100, 100);
view.transform = trans;
// For Zoom Out
CGAffineTransform trans = CGAffineTransformScale(cell.contentView.transform, 0.01, 0.01);
view.transform = trans;
To zoom complete UItableView you can apply affine transform to tableview
// For Zoom in
CGAffineTransform trans = CGAffineTransformScale(tableView.transform, 100, 100);
view.transform = trans;
// For Zoom Out
CGAffineTransform trans = CGAffineTransformScale(tableView.transform, 0.01, 0.01);
view.transform = trans;
For pinch gesture u can use following gestures well documented by apple developer documentation https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_pinch_gestures

iOS uiscrollview rotate images as the user scrolls

I have a horizontal scrollview with an image which I need to rotate as the user scrolls left or right. It needs to rotate as the user moves the scroll across the screen. So if the user scrolls half way and stops the image should rotate halfway etc. I have looked at few examples but nothing seems to be giving me the correct result. Can some one please help.
Thanks in advance
Vertical scrolling and Swift 5:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let topOffset = scrollView.contentOffset.y
let angle = -topOffset * 2 * CGFloat(Double.pi / 180)
self.myImageView.transform = CGAffineTransform(rotationAngle: angle)
}
Don't forget to do this in viewDidLoad:
self.scrollView.delegate = self
You'll need to implement the scrollViewDidScroll method and then apply a rotation matrix to the image. To rotate a UIIMage you can do what they outline here. How to Rotate a UIImage 90 degrees?
However, this would be better accomplished in a pan gesture recognizer. The pan gesture will give you information about how far the user panned, and then you can rotate the image based on that distance.
To rotate image is not good for performance. You'd better set image view's rotate transform as user scrolls. Like this:
imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

check if imageview has been rotated — Objective-c

I have an imageview that are being rotated PI/4 (radians) every time it's taped.
That works fine with this code:
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer)
{
CGAffineTransform transform = CGAffineTransformRotate(imageview.transform, (M_PI / 4));
[imageview setTransform:transform];
}
The tapRecognizer is asigned to the imageview.
Now, I want to check if the imageview has been rotated. This is my code:
if (CGAffineTransformEqualToTransform(imageview.transform, rot45)) //rot45 is a CGAffineTransformMakeRotation variable which is set to M_PI / 4
{
NSLog("Rotated");
}
That works fine for the first tap, when it has been rotated 45°. But I want to be able to check when it has been taped two times, which means that it has been rotated 90°. And so on. I want different actions on each rotation-angle. How can I check that?
Sorry if the question is unclear
Devise a scheme to map the tag property to rotation. On every rotation update the tag value.

Having a portrait label in a landscape view

I've created a .xib with a landscape orientation UIView.
The problem I'm trying to solve is that I want a UILabel running vertically along the side with text reading from bottom of the view to the top, but I can't figure out how to do that. Is that possible?
Image to show what I mean
You need to rotate and then translate it to put it where you want it. Rotation happens around the center of the label which is why you then need to translate it.
If you laid out the text label with the upper left corner where you wanted it to end up (i.e. the displayed label looks like it rotates around the upper left corner point of the label), you'd use code something like:
- (void)viewDidLayoutSubviews {
CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI_2);
CGAffineTransform transform2 = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(floor(-self.label.bounds.size.width / 2), floor(-self.label.bounds.size.width / 2)));
self.label.transform = transform2;
}
You might need to adjust the translation values slightly to get what you want, but you definitely want them to be integers (which I've done with floor) so the label is crisp.
Of course you can. You need to do a transform.
yourlabel.transform = CGAffineTransformMakeRotation(-M_PI/2);

Pinching zoom a custom UIView: lines and text are pixelate, needs to be re-rasterized

I have a custom UIView. In this view I am overriding drawRect to draw some paths and some text.
When a tap is detected, the view is zooming in
- (void)tapDetected:(UITapGestureRecognizer *)sender {
float zoom = 3.;
sender.view.transform = CGAffineTransformScale(sender.view.transform, zoom, zoom);
...
}
Zoom works OK, but lines and text become pixelate as I zoom in. I want the lines width and the text size to stay the same, i.e. to be re-rasterized, so I insert a setNeedsDisplay at the end of the above method, but this has no effect, don't works.
Any help?
Thank you.
Try changing the contentScale of the layer in question. This has worked for my paths.
I was having similar problems with my map viewer, so i cooked up an isolated demo project. I got very heavy antialiasing when zoomed in. Basically, to rerasterize when zooming in, you have to play with the rasterizationScale like so:
sublayer.rasterizationScale = scale;
sublayer.contentsScale = scale;
Sample project and readme are here: https://bitbucket.org/robvanderveer/scrollviewdemo

Resources