我试图根据用户请求在运行时动态创建视图(UIImageView和UITextView),然后允许用户移动它们并调整其大小。除了调整大小,我的所有工作都很好。我尝试使用捏手势识别器,但发现它对于我想要的东西来说太笨拙。因此,我想使用大小调整手柄。我相信我可以在每个手柄上放置一个平移手势识别器,并在其中一个移动时调整视图框架。
问题是,我不太确定如何创建大小调整手柄。我会指出我尝试过的所有内容,但说实话,我不确定从哪里开始。我确实有一些想法...
1)可能使用核心图形在拐角和侧面绘制框或圆吗?我会创建一个新图层并在其上绘制它们吗?不确定。
2)在每个角上粘贴一个方框或圆形的小图像?
3)XIB文件上已经放置了句柄?
任何建议表示赞赏。我只需要指出正确的方向。
编辑:像苹果在Pages中使用的东西将是完美的!
首先,我建议为UIView创建一个自定义View子类,您将在此处处理所有行为。我们称它为ResizableView。
在自定义视图中,您需要在角落处为这些点绘制图层或视图并向其添加PangestureRecognized,然后您可以recognizer.locationInView()
在用户拖动它们时使用这些点来跟踪这些点的位置,这将用于计算View的比例。您可以参考的代码是:
func rotateViewPanGesture(recognizer: UIPanGestureRecognizer) {
touchLocation = recognizer.locationInView(self.superview)
let center = CalculateFunctions.CGRectGetCenter(self.frame)
switch recognizer.state {
case .Began:
initialBounds = self.bounds
initialDistance = CalculateFunctions.CGpointGetDistance(center, point2: touchLocation!)
case .Changed:
//Finding scale between current touchPoint and previous touchPoint
let scale = sqrtf(Float(CalculateFunctions.CGpointGetDistance(center, point2: touchLocation!)) / Float(initialDistance!))
let scaleRect = CalculateFunctions.CGRectScale(initialBounds!, wScale: CGFloat(scale), hScale: CGFloat(scale))
self.bounds = scaleRect
self.refresh()
case:.Ended:
self.refresh()
default:break
touchLocation
location of the Pangesture
center
is the center of ResizableView
initialBounds
is the initial bounds of the ResizableView when PanGesture begin.
initailDistance
is the distance between the center of the ResizableView of touchPoint of the dot the user is dragging.
Then you can calculate the scale
given initialDistance
, center
, touch location
Now you have scaled the view as You want. You also need to refresh the position of these dot at corner accordingly, that's what refresh()
for, you need to implement it yourself.
I tend to define some helpFunctions to help me calculate.
CalculateFunctions.CGPointGetDistance
is used to calculate the distance between center of the view and touch location.
CalculateFunctions.CGRectScale
return the scaled CGRect given the the scale you just calculated.
CalculateFunctions.CGRectGetCenter
return the center point of a CGRect
那只是一个粗略的主意。实际上,您可以参考许多图书馆。一些建议:
如有任何疑问,请随时发表。祝你好运:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句