将滑块设为UIPanGesturerecognizer

时间:2016-02-19 22:02:20

标签: ios uigesturerecognizer uislider uipangesturerecognizer

我使用UIPanGestureRecognizer创建了一个自定义滑块。问题是自定义滑块有滞后。观看此视频:https://vid.me/CtME。我点击并拖动自定义“3km”,但它只在一段距离之后开始移动而不是马上开始移动。 “1km”UISlider没有滞后。我该如何解决这个问题?

代码:

thumb = UIImageView(frame: CGRectMake(x, y, width, height))
        thumb.image = UIImage(named: "3km")
        thumb.userInteractionEnabled=true
        backgrounImageView.addSubview(thumb)

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "pan:")
        thumb.addGestureRecognizer(panGestureRecognizer)


func pan(gestureRecognizer:UIPanGestureRecognizer){
        let vv = gestureRecognizer.view
        print((vv?.superview?.frame.width)!)
        print("x: \(gestureRecognizer.locationInView(vv?.superview).x)")
        let x = gestureRecognizer.locationInView(vv?.superview).x

        if (x < (vv?.superview?.frame.width)!*0.11 || x > (vv?.superview?.frame.width)!*0.89){
            return
        }


        switch gestureRecognizer.state {

        case .Began, .Changed:
            let delta = gestureRecognizer.translationInView(vv?.superview)
            var c = vv?.center
            c!.x += delta.x
            vv?.center = c!
            gestureRecognizer.setTranslation(CGPointZero, inView: vv?.superview)

        default:
            break

        }
    }

0 个答案:

没有答案