[英](Scale) Zoom into a UIView at a point
我不了解轉化的東西。 我想放大一下UIView的右上角(主視圖)。 我使用CGAffineTransformScale
並嘗試設置center / 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) {}];
您正在將視圖的中心設置在右上角。 這意味着你要放大到中心左下角的某個地方。
嘗試這個
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) {}];
這會將放大視圖的中心設置為左下角,有效地放大,右上角固定。
此代碼沒有硬編碼的高度,這是一個更便攜(到iPhone 5的iPad)。
您需要先設置transform
屬性之前保存h
,因為之后您不應該依賴於frame
的值。
編輯
要使其適用於任何比例s
,請使用以下命令:
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-w*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,h-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-w*s/2,h-h*s/2);
} completion:^(BOOL finished) {}];
如果有人仍然感興趣,這里是Swift 3.0解決方案,我添加了正確淡出的可能性。
(我知道有兩個枚舉缺失,但代碼仍然可讀,我想你會得到這個概念)
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 = h-h*scale/2
break
case .LEFT_UP:
cx = w*scale/2
cy = h*scale/2
break
case .RIGHT_UP:
cx = w-w*scale/2
cy = h*scale/2
break
case .RIGHT_DOWN:
cx = w-w*scale/2
cy = h-h*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 })
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.