【问题标题】:XCode/Swift: how to manage constraints of two overlapping imageviewsXCode/Swift:如何管理两个重叠图像视图的约束
【发布时间】:2016-02-21 20:09:26
【问题描述】:

在我的应用程序中,我想在更大的 UIImageView 上管理一个可拖动的 pin,即 UIImageView。这是我故事板中的截图:

问题是我无法管理约束:我希望 pin 图像只能在下面的大 UIImageView 内拖动。 您对如何编写这些约束有任何想法吗?

【问题讨论】:

    标签: ios xcode uiimageview constraints draggable


    【解决方案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
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 2018-07-11
      • 2021-12-26
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      相关资源
      最近更新 更多