XCode/Swift:如何管理两个重叠图像视图的约束
Posted
技术标签:
【中文标题】XCode/Swift:如何管理两个重叠图像视图的约束【英文标题】:XCode/Swift: how to manage constraints of two overlapping imageviews 【发布时间】:2016-02-21 20:09:26 【问题描述】:在我的应用程序中,我想在更大的 UIImageView 上管理一个可拖动的 pin,即 UIImageView。这是我故事板中的截图:
问题是我无法管理约束:我希望 pin 图像只能在下面的大 UIImageView 内拖动。 您对如何编写这些约束有任何想法吗?
【问题讨论】:
【参考方案1】:更新:回答
在Main.storyboard
中不需要约束,总体而言,有必要将每个引脚实现为UIImageView
的子类和UIPanGestureRecognizer
。这是我将允许引脚移动的尺寸编码到的类:
import UIKit
class PinImageView: UIImageView
var lastLocation:CGPoint?
var panRecognizer:UIPanGestureRecognizer?
init(imageIcon: UIImage?, location:CGPoint)
super.init(image: imageIcon)
self.lastLocation = location
self.panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
self.center = location
self.gestureRecognizers = [panRecognizer!]
self.frame = CGRect(x: location.x, y: location.y, width: 20.0, height: 30.0)
self.userInteractionEnabled = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
func detectPan(recognizer:UIPanGestureRecognizer)
let translation = recognizer.translationInView(self.superview!)
let newLocation = CGPointMake(lastLocation!.x + translation.x, lastLocation!.y + translation.y)
if ((newLocation.x >= 26 && newLocation.x <= 292) && (newLocation.y >= 71 && newLocation.y <= 461))
self.center = CGPointMake(newLocation.x, newLocation.y)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
// Promote the touched view
self.superview?.bringSubviewToFront(self)
// Remember original location
lastLocation = self.center
【讨论】:
以上是关于XCode/Swift:如何管理两个重叠图像视图的约束的主要内容,如果未能解决你的问题,请参考以下文章
在一个视图控制器中使用不同的按钮上传多个图像(iOS、Xcode 9、Swift 4)