简体   繁体   English

水平缩放背景彩色图像

[英]horizontally scaling a background color image

I am new to iOS and Swift. 我是iOS和Swift的新手。 I am trying to add a background image for a slide right effect. 我正在尝试为幻灯片右添加效果的背景图像。 I want that image to stick to the right of the screen no matter what orientation. 我希望该图像在任何方向都可粘贴在屏幕的右侧。 The image will always be a set size (now its (382x145px). How can I adjust it on landscape orientation so that the picture stays to the right. 图像将始终为设定的尺寸(现在为(382x145px)。如何调整横向方向,以使图像保持在右侧。

Here is my custom view cell class. 这是我的自定义视图单元类。

class CustomRouteViewCell: UITableViewCell {

    @IBOutlet var downImage: UIImageView!
    @IBOutlet var downTime: UILabel!
    @IBOutlet weak var leadingSpaceConstraint: NSLayoutConstraint!
    @IBOutlet weak var trailingSpaceConstraint: NSLayoutConstraint!

    @IBOutlet var locationTitle: UILabel!
    @IBOutlet weak var panView: UIView!

    @IBOutlet var timerLabel: UILabel!
    var indexRow: Int = 0
    var timeInterval: Int = 0

    override func awakeFromNib() {
        super.awakeFromNib()

        self.selectedBackgroundView = nil

        var img = UIImage(named: "tableSwipeArrow.png")!

        self.panView.backgroundColor = UIColor(patternImage: img)



        if self.respondsToSelector(Selector("setLayoutMargins:")) {
            layoutMargins = UIEdgeInsetsZero
        }

        if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
            preservesSuperviewLayoutMargins = false
        }


        var panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
        panGestureRecognizer.delegate = self
        addGestureRecognizer(panGestureRecognizer)
    }

    func handlePanGesture(recognizer: UIPanGestureRecognizer) {

        switch(recognizer.state) {

        case UIGestureRecognizerState.Changed:

            var translation = recognizer.translationInView(recognizer.view!)
            var little = CGFloat(trailingSpaceConstraint.constant  + translation.x * -1)
            trailingSpaceConstraint.constant = fmax(0, little)
            leadingSpaceConstraint.constant = fmin(0, leadingSpaceConstraint.constant + translation.x)
            recognizer.setTranslation(CGPointZero, inView: recognizer.view!)

            timeInterval = Int(trailingSpaceConstraint.constant / 30) * 5
            timerLabel.text! = "\(timeInterval)"

        case UIGestureRecognizerState.Ended, UIGestureRecognizerState.Cancelled:

            var refreshAlert = Alarm.getReminderView(self.timeInterval, view: self.parentViewController!.view)

            self.parentViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
            leadingSpaceConstraint.constant = 0
            trailingSpaceConstraint.constant = 0

            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.layoutIfNeeded()
            })

        default:
            trailingSpaceConstraint.constant = 0
            leadingSpaceConstraint.constant = 0
        }
    }

}


extension CustomRouteViewCell: UIGestureRecognizerDelegate
{
    override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {

        if gestureRecognizer.isKindOfClass(UIPanGestureRecognizer) {

            var velocity = (gestureRecognizer as! UIPanGestureRecognizer).velocityInView(gestureRecognizer.view!)

            return fabs(velocity.x) > fabs(velocity.y)
        }

        return true;
    }
}

在此处输入图片说明在此处输入图片说明

Use auto layout to link the position of the images. 使用自动布局链接图像的位置。 In your case you can connect the trailing of the background image with the container view. 您可以将背景图片的尾部与容器视图连接起来。

Hold Control on image and link the Trailing Attribute with the main view 按住图片控件,然后将尾随属性与主视图链接

Just adjust the constant to 0. 只需将常数调整为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM