2017-04-18 10 views
1

현재 UIView의 터치 수에 따라 UIView의 색을 변경하고 싶습니다. 나는 한 번의 터치로 이것을 만들 수 있었다 :UIView를 터치하는 손가락 수 얻기

class colorChangerViewClass: UIView { 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     setColor(color: .blue) 
     print("touchesBegan") 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     setColor(color: .green) 
     print("touchesEnded") 
    } 


    func setColor(color: UIColor) { 
    self.backgroundColor = color 
    } 
} 

그러나 나는 멀티 터치를 구현하는 데 어려움을 겪고있다. 내가 뭔가를 이해하지 못하는 것 같아.

UIView에는 현재 손가락 몇 개를 터치했는지 확인할 수있는 속성이 있습니까?

새로운 touchesBegan 또는 touchesEnded가 발생할 때마다이를 확인할 수 있습니다.

답변

4

먼저 당신이 당신의 UIView 클래스 이벤트와 모든 접촉을 얻을 수있는 터치 이벤트 함수 내부

self.isMultipleTouchEnabled = true 

그런 다음 서브 클래스에 멀티 터치를 할 수 있습니다.

let touchCount = event?.touches(for: self)?.count 
+0

'이벤트를 변경 (대한 : 자기) .touches?의'UITouch 설정이 아니라 내가 다시 – Marmelador

+0

에 접촉의 양을 반환 이제 새로운 문제가 발생합니다 : UIView에서 손가락 두 개를 들어 올리면 하나의 이벤트로 계산됩니다. 따라서 '이벤트? .touches (for : self)? count.'에 대해 질의 될 때'touchesEnded'를 트리거하지만'2'를 리턴합니다. 따라서 UIView에 두 손가락을 대고 손가락 하나를 들게하는 것과 구별 할 수 없게 만듭니다. – abdullahselek

+0

I를 카운트 확인하시기 바랍니다 추가 잊은 UIView의 – Marmelador

1

UIViewisMultipleTouchEnabled = true이 있는지 확인은 기본 false입니다.

그런 다음 touchesBegan 방법을

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     setColor(color: .blue) 
     let touchCount = event?.touches(for: self)?.count 
    }