水平缩放背景颜色图像

时间:2015-06-18 10:35:55

标签: ios swift uiimage

我是iOS和Swift的新手。我正在尝试为幻灯片右侧效果添加背景图像。无论方向如何,我都希望该图像能够贴在屏幕右侧。图像将始终为设定尺寸(现在为(382x145px)。如何在横向方向上调整图像,使图像保持在右侧。

这是我的自定义视图单元类。

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;
    }
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

使用自动布局链接图像的位置。 在您的情况下,您可以将背景图像的尾部连接到容器视图。

保持对图像的控制并将尾随属性与主视图链接

只需将常数调整为0。