我正在创建一个显示时间轴的控件。最初,它应该显示年份。放大并且zoomScale
达到特定值时,它将显示各个月的值,而在下一个缩放级别,它应显示各个日期的实际值。
我创建了一个scrollview
并添加了layers
(用于显示月/年值)。在覆盖了压缩信息后,我可以进行缩放(普通UIScrollview缩放)。但我需要将其缩放到特定点。即,假设我正在缩放1月,我需要将它保持在相同的位置(用于创建像在1月内移动的效果)。有任何建议可以达到这个目的吗?
我的方式是当前的吗?否则请帮我开始吧。
答案 0 :(得分:3)
根据您的要求使用此波纹管方法,
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
只需用你选择的框架或点调用上面的方法,就像选择1月那样只需要获取1月按钮或视图的点(位置),然后用该框架调用上面的方法..
有关详细信息,请参阅此链接UIScrollView_Class
我希望这对你有帮助......
答案 1 :(得分:2)
这是它的样子:
@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
观察它,弄清楚它是如何工作的应该是非常简单的。如果您发现任何问题,请在评论中告诉我 谢谢:))
来源:http://www.tim-oliver.com/2012/01/14/zooming-to-a-point-in-uiscrollview/
答案 2 :(得分:0)
我有同样的问题,
我有一张世界地图图片,&我想将地图缩放到某个位置,印度
这就是我修复它的方法
我发现了印度的位置,就CGRect(X,Y)而言[CGRect(500,300)]
&安培;以下代码在DidLoad
[self.scrollViewForMap setZoomScale:2.0 animated:YES]; //for zooming
_scrollViewForMap.contentOffset = CGPointMake(500,300); //for location
这解决了我的问题。 :)
答案 3 :(得分:0)
- (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
}
}
答案 4 :(得分:0)
什么对我有用( Swift 4 ):
func zoomIn(with gestureRecognizer: UIGestureRecognizer?) {
guard let location = gestureRecognizer?.location(in: gestureRecognizer?.view)
let rect = CGRect(x: location.x, y: location.y, width: 0, height: 0)
scrollView.zoom(to: rect, animated: true)
}
或者换句话说,创建宽度和高度为零的CGRect
将导致UIScrollView
缩放到origin
处的maximumZoomScale