(Scale)在一个点放大到一个UIView
一些我不了解转换的东西。 我想放大说一个UIView(主视图)的右上angular。 我使用CGAffineTransformScale
并试图设置中心/ anchorPoint以及CGAffineTransformMakeTranslation
无济于事。
我无法弄清楚如何正确设置翻译,以便放大这一点。
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, 2, 2); [UIView animateWithDuration:2.5 delay:0 options:0 animations:^{ self.view.transform = tr; self.view.center = CGPointMake(480,0); } completion:^(BOOL finished) {}];
您正在将视图的中心设置在右上angular。 这意味着你正在放大中心左下angular的某个地方。
尝试这个
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, 2, 2); CGFloat h = self.view.frame.size.height; [UIView animateWithDuration:2.5 delay:0 options:0 animations:^{ self.view.transform = tr; self.view.center = CGPointMake(0,h); } completion:^(BOOL finished) {}];
这将把放大视图的中心设置到左下angular,有效地放大固定右上angular。
此代码没有硬编码的高度,这是一个更便携(iPhone 5的iPad)。
在设置transform
属性之前,您需要先保存h
,因为之后不应该依赖frame
的值。
编辑
要使它适用于任何比例尺,请使用以下命令:
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) {}];
为了使它在左下方工作,使用这个:
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(w*s/2,hh*s/2); } completion:^(BOOL finished) {}];
为了使它适用于右下angular,使用这个:
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,hh*s/2); } completion:^(BOOL finished) {}];
另请参阅: 如何将UIView缩放(缩放)到给定的CGPoint
如果有人仍然感兴趣,这里是Swift 3.0解决scheme,我添加了正确淡出的可能性。
(我知道有两个枚举丢失,但代码仍然可读,我想你会得到这个概念)
func animateZoom(direction: ZoomDirection, type: ZoomType, duration: Double = 0.3, scale: CGFloat = 1.4) { var cx, cy: CGFloat var tr: CGAffineTransform = CGAffineTransform.identity if (type == .IN) { tr = self.transform.scaledBy(x: scale, y: scale) } let h: CGFloat = self.frame.size.height; let w: CGFloat = self.frame.size.width; if (type == .IN) { switch direction { case .LEFT_DOWN: cx = w*scale/2 cy = hh*scale/2 break case .LEFT_UP: cx = w*scale/2 cy = h*scale/2 break case .RIGHT_UP: cx = ww*scale/2 cy = h*scale/2 break case .RIGHT_DOWN: cx = ww*scale/2 cy = hh*scale/2 break } } else { cx = w/scale/2 cy = h/scale/2 } UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseInOut], animations: { self.transform = tr self.center = CGPoint(x: cx, y: cy) }, completion: { finish in }) }
- 它是如何animationUINavigationBar的条形颜色的变化,而不是一个UITabBar?
- 更改UIImageView图像翻转animation的一半
- 两个贝塞尔path形状之间的animation
- 认定animation类似于在iOS 7中打开应用程序:viewcontroller框架不能resize
- 更改正在运行的animation的持续时间(速度)
- UIViewController包含animation,如Google+
- animationUICollectionView contentOffset不显示不可见的单元格
- 如何知道视图是否可见?
- 如何使UIScrollView在animation过程中发送scrollViewDidScroll消息