How can I increase the scaling of the UIScrollview to a fixed point?
I am creating a control to display a timeline. It should initially show the year. When the scaling and has zoomScale
reached a certain value, it will show the value of the individual months, and at the next level of scaling it should show the real values for the individual days.
I created a file scrollview
and added layers
(to display month / year values). After I override the pinch program, I can do the scaling (UIScrollview normal scaling). But I need to zoom in to a certain point. those. Suppose I am increasing January, I need to keep it in the same position (to create an effect like moving in January). Any suggestions to achieve this?
Am I trying to do it the right way? otherwise, please help me start this.
source to share
Use this below method for your requirement,
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
just call this above method with a frame or point selected, for example, if you select January, just take the point (position) of a button or button view, or just call the method above with that frame.
for more information see this link UIScrollView_Class
Hope this helps you ...
source to share
This is what it looks like:
@implementation UIScrollView (ZoomToPoint)
- (void)zoomToPoint:(CGPoint)zoomPoint withScale: (CGFloat)scale animated: (BOOL)animated
{
//Normalize current content size back to content scale of 1.0f
CGSize contentSize;
contentSize.width = (self.contentSize.width / self.zoomScale);
contentSize.height = (self.contentSize.height / self.zoomScale);
//translate the zoom point to relative to the content rect
zoomPoint.x = (zoomPoint.x / self.bounds.size.width) * contentSize.width;
zoomPoint.y = (zoomPoint.y / self.bounds.size.height) * contentSize.height;
//derive the size of the region to zoom to
CGSize zoomSize;
zoomSize.width = self.bounds.size.width / scale;
zoomSize.height = self.bounds.size.height / scale;
//offset the zoom rect so the actual zoom point is in the middle of the rectangle
CGRect zoomRect;
zoomRect.origin.x = zoomPoint.x - zoomSize.width / 2.0f;
zoomRect.origin.y = zoomPoint.y - zoomSize.height / 2.0f;
zoomRect.size.width = zoomSize.width;
zoomRect.size.height = zoomSize.height;
//apply the resize
[self zoomToRect: zoomRect animated: animated];
}
@end
Looking at it, it should be pretty easy to figure out how it works. If you see anything wrong with this, let me know in the comments.
Thank:)
source: http://www.tim-oliver.com/2012/01/14/zooming-to-a-point-in-uiscrollview/
source to share
I had the same problem,
I had an image of a world map and I wanted to enlarge the map to a specific location, India
This is how I fixed it.
I learned India's position regarding CGRect (X, Y) [CGRect (500 300)]
& amp; following code in DidLoad
[self.scrollViewForMap setZoomScale:2.0 animated:YES]; //for zooming
_scrollViewForMap.contentOffset = CGPointMake(500,300); //for location
This solved my problem. :)
source to share
- (void)pinchDetected:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.2;
const CGFloat kMinScale = 0.64;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
[gestureRecognizer setScale:1.0];
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
source to share