2017-09-22 3 views
0

다음 코드가 있지만 시도한 모든 것 외에도 제스처는 swipeUpDots에서 인식되지 않습니다 (콘솔에 "스 와이프"되지 않음). 고맙습니다.UIImageView의 신속한 스 와이프 제스처가 전혀 작동하지 않습니다.

viewDidLoad 함수에 setupInputComponents()가 있는데 올바르게 호출됩니다.

func setupInputComponents() { 

    let containerView = UIView() 

    view.addSubview(containerView) 

    let swipeUpDots = UIImageView() 

    swipeUpDots.image = #imageLiteral(resourceName: "threeDots") 

    swipeUpDots.contentMode = .scaleAspectFit 

    containerView.addSubview(swipeUpDots) 

    // Adding swipe functionality 

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(getSwipedUp(swipeGestureRecognizer:))) 

    swipeUpDots.addGestureRecognizer(swipeUp) 

    swipeUpDots.isUserInteractionEnabled = true 

} 

@objc func getSwipedUp(swipeGestureRecognizer: UISwipeGestureRecognizer){ 

    print("swiped") 

    if swipeGestureRecognizer.direction == .up { 

     print("Up Swiped") 

     // Send Message 



    } 

} 

답변

1

UISwipeGestureRecognizer의 허용 방향을 설정해야합니다. 귀하의 경우 :

swipeUp.direction = .up 

또한, 당신이 자신의 프레임을 정의하지 않고 containerView (UIView의)와 swipeUpDots (있는 UIImageView)을 초기화 한 것으로 나타났습니다. 프로그래밍 방식으로 새 UIView를 만들 때 시스템에 그리기 위치와 사각형 크기를 알려줘야합니다. 다음은 예입니다.

let rect = CGRect(x: 100, y: 100, width: 100, height: 100) 
let containerView = UIView(frame: rect) 

imageView의 경우 frame 속성을 설정하십시오. 자세한 내용은 애플에서 문서를 참조하십시오 :

UISwipeGestureRecognizer

UIView

UIImageView

+0

가 실제로 * 더 *, 당신은 * *는'UIView'를 초기화보다 아무것도 더 많은 일을 할 필요 없어 자동 레이아웃을 사용하는 경우. (이 경우 뷰의'translatesAutoresizingMaskIntoConstraints'를'false'로 설정해야합니다.) – dfd

+0

OP가 자동 레이아웃을 사용하고 있는지 확실하지 않습니다. 그러나 나를 위해 그것을 비우는 것에 대해 감사합니다! –

관련 문제