2016-09-04 12 views
1

요소 위에 클릭하고 끌어 올 때 여러 이미지를 업데이트하려고합니다. 나는 touchesbegan, touchesmoved 및 touchesended를 구현했지만, touchesmoved 효과를 여러 이미지에 적용하는 방법을 모르겠습니다.Touchesmoved - 단일 드래그에서 여러 요소 업데이트

나는 웹을 샅샅이 뒤졌지만, 이것에 대한 안내서를 찾을 수 없었다. 당신이 올바른 방향으로 나를 가르키거나 몇 가지 기본적인 충고를 해주면 크게 감사 할 것입니다.

편집 : 다음

예제 이미지 다음이 가능해야하는지의 예 이미지 :

What it should look like.

나는 효과 수 있도록하고 싶습니다 다른 편지는 동일한 언론을 통해 같은 방식으로 사진을 변경합니다. 이 그림은 임시 이미지입니다.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    print("touches began:\(touches)") 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      self.image = UIImage(named: "slot")! 
     print("touches moved:\(touches)") 
    } 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self.image = UIImage(named: "tile")! 
     print("touches ended") 
    } 
+0

어떻게 작동해야합니까? – iyuna

+0

작동 방법은 각 요소 위로 손가락을 끈 다음 E와 같은 그림에서 각각의 S와 같은 그림으로 변경하고 각 문자의 값을 가져옵니다. 위의 수퍼 뷰에서이 작업을 수행하는 것이 바람직 할 수 있다고하는 기사가 하나 있습니다. 나는 그것이 어떻게 생겼는지에 대한 예를 추가했습니다. – Apocal

답변

1

내가 제대로 이해하면, 그 결과는 아래와 같습니다 : 당신의 UIViewController가보기로 ChangableView의 서브 클래스를 가지고 있어야하고 userInteractionEnabled 속성을 설정하는 것을 잊지 마세요 또한

class ChangableView: UIView { 
    private var touchedImageView: UIImageView? { 
     didSet { 
      if oldValue == touchedImageView { return } 
      if let oldValue = oldValue { 
       oldValue.image = UIImage(named: "tile")! 
      } 
      if let newValue = touchedImageView { 
       newValue.image = UIImage(named: "slot")! 
      } 
     } 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     guard let touch = touches.first else { return } 
     updateTouchedImageViewWithTouch(touch, event: event) 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     guard let touch = touches.first else { return } 
     updateTouchedImageViewWithTouch(touch, event: event) 
    } 

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     touchedImageView = nil 
    } 
} 

private extension ChangableView { 
    func updateTouchedImageViewWithTouch(touch: UITouch, event: UIEvent?) { 
     let touchPoint = touch.locationInView(self) 
     let touchedView = self.hitTest(touchPoint, withEvent: event) 
     touchedImageView = touchedView as? UIImageView 
    } 
} 

모든 YES로 UIImageViews.

+0

감사합니다. – Apocal