0

Размер изображения должен зависить от времени зажатия кнопки. У меня размер изменяется в начале зажатия и все, а надо ,чтобы он относительно времени зажатия изменялся.

var startTime = Date()
@IBOutlet weak var button: UIButton!
@IBOutlet weak var heartImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longPresss = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPresss.delegate = self
button.addGestureRecognizer(longPresss)

}


func longPress(gesture: UILongPressGestureRecognizer) {

print(heartImageView.frame.size)
if gesture.state == .began {
    let duration = Date().timeIntervalSince(startTime)
    print("duration  \(Int(duration)) seconds")

    UIView.animate(withDuration: 0.5, animations: {
        if Int(duration) < 3 {
            self.heartImageView.transform = CGAffineTransform(scaleX: 1.35, y: 1.35)

        } else {
            self.heartImageView.alpha = 0
        }
        }, completion: nil)

}
else if gesture.state == .ended {
    let duration = Date().timeIntervalSince(startTime)
    print("duration was \(Int(duration)) seconds")

    }
}
2
  • а вопрос в чем? 5 фев 2017 в 17:30
  • У меня размер изображения изменяется в начале зажатия (в первые 2 секунды), а мне надо, чтобы размер изображения изменялся пропорционально времени зажатия
    – Waver
    5 фев 2017 в 17:38

1 ответ 1

0

Исходя из Apple documentation: Long-press gestures are continuous. The gesture begins (began) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (ended) when any of the fingers are lifted.

Дефолтное значение allowableMovement - 10, попробуй увеличить, может при нажатии палец сдвигается за пределы 10 point, а также посмотреть прилетает ли gesture.state == .cancelled

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.