在两个方向调整视图大小?
Posted
技术标签:
【中文标题】在两个方向调整视图大小?【英文标题】:Resizing a view in two directions? 【发布时间】:2017-03-11 12:12:37 【问题描述】:在 Swift 中我有一个问题。我知道如何使用手势来调整视图的大小,但我想做的是让用户在两个不同的方向上缩放矩形视图。如果我想增加矩形的宽度,我会左右捏合。如果我想增加长度捏起来。你得到图片。到目前为止,这是我的代码。我不确定如何将我的伪代码转换为真实代码。
func axisFromPoints(p1: CGPoint, _ p2: CGPoint) -> String
let x_1 = p1.x
let x_2 = p2.x
let y_1 = p1.y
let y_2 = p2.y
let absolutePoint = CGPoint(x: x_2 - x_1, y: y_2 - y_1)
let radians = atan2(Double(absolutePoint.x), Double(absolutePoint.y))
let absRad = fabs(radians)
if absRad > M_PI_4 && absRad < 3*M_PI_4
return "x"
else
return "y"
func handlePinch(_ sender : UIPinchGestureRecognizer)
//var p1 = location(ofTouch 0,in:sender)
//var p2 = location of touch 1
//call function
// if function == 'x'
//view.transformed.scaledBy(x:sender.scale,y:1)
//if function == 'y'
//view.transformed.scaledBy(x:1,y:sender.scale)
if let view = sender.view
view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
【问题讨论】:
github.com/zedoul/ZDStickerView 。请使用这个而不是编写自己的控件 【参考方案1】:我是这样做的。
func axisFromPoints(p1: CGPoint, _ p2: CGPoint) -> String
let x_1 = p1.x
let x_2 = p2.x
let y_1 = p1.y
let y_2 = p2.y
let absolutePoint = CGPoint(x: x_2 - x_1, y: y_2 - y_1)
let radians = atan2(Double(absolutePoint.x), Double(absolutePoint.y))
let absRad = fabs(radians)
if absRad > M_PI_4 && absRad < 3*M_PI_4
return "x"
else
return "y"
func handlePinch(_ sender : UIPinchGestureRecognizer)
if sender.state == .changed
let p1: CGPoint = sender.location(ofTouch: 0, in: view)
let p2: CGPoint = sender.location(ofTouch: 1, in: view)
var x_scale = sender.scale;
var y_scale = sender.scale;
if axisFromPoints(p1,p2) == "x"
y_scale = 1;
x_scale = sender.scale
if axisFromPoints(p1, p2) == "y"
x_scale = 1;
y_scale = sender.scale
view.transform = view.transform.scaledBy(x: x_scale, y: y_scale)
sender.scale = 1
【讨论】:
以上是关于在两个方向调整视图大小?的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollViews 子视图没有在纵向到横向方向更改上调整自己的大小
方向更改后的 UIView addsubview:告诉视图调整大小