简体   繁体   中英

horizontally scaling a background color image

I am new to iOS and 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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