如何将UIView缩放(缩放)到给定的CGPoint

我花了很多时间试图find一种方法来使用CGAffineScale将视图转换为给定的点,包括混合锚点,在转换之前和之后移动视图的中心和全面的谷歌search。 我知道这将是一个简单的UIScrollview; 但是我知道在没有人的情况下,技术上是可能的,而且在我看来这已经变成一个分裂。

这个答案非常接近我想要达到的目标,但是答案只是给出了如何通过巧妙地将中心移动到要放大的那个angular落的对angular来放大给定angular落(而不是给定点)的细节。

我怎样才能修改mvds的代码来缩放UIView到UIView中的任何给定的点?

CGFloat s = 3; CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s); CGFloat h = self.view.frame.size.height; CGFloat w = self.view.frame.size.width; [UIView animateWithDuration:2.5 delay:0 options:0 animations:^{ self.view.transform = tr; self.view.center = CGPointMake(ww*s/2,h*s/2); } completion:^(BOOL finished) {}]; 

涉及2个步骤:首先放大要放大的视图。 然后,将这个放大的视图的中心设置为使得您想要看到的部分在视图中间结束。

你应该在纸上画出来,公式如下:(未经testing)

 CGFloat s = 3; CGPoint p = CGPointMake(100, 200); CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s); CGFloat h = self.view.frame.size.height; CGFloat w = self.view.frame.size.width; [UIView animateWithDuration:2.5 delay:0 options:0 animations:^{ self.view.transform = tr; CGFloat cx = w/2-s*(px-w/2); CGFloat cy = h/2-s*(py-h/2); self.view.center = CGPointMake(cx, cy); //was: (w*s/2,hh*s/2); } completion:^(BOOL finished) {}]; 

我自己也碰到了这个同样的问题。 为了解决这个问题,我所做的只是改变了我缩放的视图的锚点,因为CGAffineTransforms是在视图上执行的,因为CGAffineTransforms是相对于它的锚点执行的,所以根据锚点的位置,变换会缩放,平移或旋转视图不同。 这是基本的想法:

 CGPoint pointToScaleTo = CGPointMake(x, y); //Just enter the coordinates you //want to scale your view towards CGFloat viewWidth = self.view.bounds.size.width; CGFloat viewHeight = self.view.bounds.size.height; CGFloat scaleFactorX = ...; CGFloat scaleFactorY = ...; CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactorX, scaleFactorY); [UIView animateWithDuration:2.5f delay:0.0f options:0 animations:^{ //I divide the x and y coordinates by the view width and height //because the anchor point coordinates are normalized to the range //0.0-1.0. self.view.layer.anchorPoint = CGPointMake(pointToScaleTo.x/viewWidth, pointToScaleTo.y/viewHeight); //Now that the anchor point has been changed, apply the scale transform self.view.layer.transform = scaleTransform; } completion:^(BOOL finished) {}];