对于横向和纵向模式,是否可以将元素重置为相同的%位置(距离x = 0轴的距离)?如果是,你可以告诉我怎么样?另外,因为如果你发现任何不良代码错误我是Swift的新手,请 DO 通知我以便改善。谢谢提前
这是我的ViewController
:
import UIKit
class ViewController: UIViewController
{
@IBOutlet var initView: UIView!
{
didSet
{
initView.addGestureRecognizer(UIPanGestureRecognizer(target: initView, action: "panRecognize:"))
}
}
}
这是我的View
:
import UIKit
class InitView: UIView {
private struct location
{
static var movableRect : CGPoint = CGPoint.zero
static var movableRectSize : CGSize = CGSize(width: location.screensize.width*0.2, height: location.screensize.height*0.05)
static var screensize : CGRect = UIScreen.mainScreen().bounds
}
func panRecognize(gesture : UIPanGestureRecognizer)
{
let state = gesture.state
switch state
{
case .Ended:
fallthrough
case .Changed:
gesture.setTranslation(gesture.locationInView(self), inView: self)
location.movableRect.x = gesture.translationInView(self).x - location.movableRectSize.width/2
location.movableRect.y = gesture.translationInView(self).y - location.movableRectSize.height/2
setNeedsDisplay()
default:
break
}
}
private func makeMovableRect(arg: CGPoint)
{
let path = UIBezierPath(rect: CGRect(origin: arg, size: location.movableRectSize))
UIColor.purpleColor().set()
path.fill()
}
override func drawRect(rect: CGRect)
{
if location.movableRect != CGPoint(x: 0, y: 0)
{
makeMovableRect(location.movableRect)
}
}
}
我的程序所做的只是在用户轻敲的任何地方传输一个小的Rect并继续按下。当用户移除触摸屏幕时,对象保持到最后一个位置(默认行为)。我改变了旋转,但边界改变了我希望它重绘到与纵向模式相同的%位置。让我给你看一些照片来帮助你。也许它真的很容易但是在网上寻找解决方案让我尝试让我使用UIDevice
(isLandscape
)的bool值,我有点搞砸了。
答案 0 :(得分:0)
您可以使用
之类的功能func pointForBounds(point: CGPoint,originalBounds: CGRect)->CGPoint{
return CGPointMake(originalBounds.origin.x/self.bounds.origin.x*point.x, originalBounds.origin.y/self.bounds.origin.y*point.y)
}
在更改视图边界期间更新rect的原点