There is a requirement that can be scaled and moved in a common view in my project. But move the subview event, I need to implement
touchesMoved:withEvent:
method. So, the scale event was hijacked.This time we need to touch through.
Idea is very simple, we just need to tell the program which view to respond to events. This time we need hitTest:withEvent:
method.
The function hitTest:withEvent:
method is to find out what the view is under the touch point. Then according to our own logic to determine which view to respond to events.
views within views hierarchy, as follows:
Put your finger inside D. Here's what will happen:
-
hitTest:withEvent:
is called onA
, the top-most view of the view hierarchy. -
pointInside:withEvent:
is called recursively on each view. -
pointInside:withEvent:
is called onA
, and returnsYES
. -
pointInside:withEvent:
is called onB
, and returnsNO
. -
pointInside:withEvent:
is called onC
, and returnsYES
. -
pointInside:withEvent:
is called onD
, and returnsYES
. - On the views that returned
YES
, it will look down on the hierarchy to see the subview where the touch took place. In this case, fromA
,C
andD
, it will beD
. -
D
will be the hit-test view.
That's all.
Now, let's look at a simple demo.
The views hierarchy of storyboard, as follows:
The back view is my custom view, it is implementation as follows:
BackgroundView.swift
import Foundation
import UIKit
class BackgroundView: UIView {
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
// Find out what the view is under the touch point
let hitView = super.hitTest(point, withEvent: event)
// determine which view to respond to events
if let customView = self.subviews.last as? CustomView {
let movedViewPoint = customView.movedView.convertPoint(point, fromView: self)
let moved2ViewPoint = customView.movedView2.convertPoint(point, fromView: self)
if customView.movedView.pointInside(movedViewPoint, withEvent: event) {
customView.userInteractionEnabled = true
return customView.movedView
} else if customView.movedView2.pointInside(moved2ViewPoint, withEvent: event) {
customView.userInteractionEnabled = true
return customView.movedView2
}
}
return hitView;
}
}
We need implement touchesMoved:withEvent:
in custom view class.
CustomView.swift
import Foundation
import UIKit
class CustomView: UIView {
var movedView: UIView!
var movedView2: UIView!
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, withEvent: event)
if hitView != self.movedView {
// Close user interaction, this time image view can be scaled
self.userInteractionEnabled = false
}
return hitView
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let allTouches = event?.allTouches() {
if let touch = (allTouches as NSSet).anyObject() {
let optionView: UIView? = touch.view
if optionView != nil {
if optionView!.tag < 101 {
return
}
let point = touch.locationInView(self)
optionView!.center = point
}
}
}
}
func addMovedViewInView() {
self.movedView = UIView(frame: CGRectMake(self.center.x, self.center.y, 100, 100))
self.movedView.tag = 101
self.movedView.layer.cornerRadius = 50.0
self.movedView.backgroundColor = UIColor.blackColor()
self.addSubview(self.movedView)
self.movedView2 = UIView(frame: CGRectMake(50, 50, 100, 100))
self.movedView2.tag = 102
self.movedView2.layer.cornerRadius = 50.0
self.movedView2.backgroundColor = UIColor.orangeColor()
self.addSubview(self.movedView2)
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var backView: BackgroundView!
@IBOutlet weak var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self;
self.scrollView.minimumZoomScale = 1.0;
self.scrollView.maximumZoomScale = 3.0;
self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true)
self.imageView = UIImageView(image: UIImage(named:"testImage"))
self.imageView.frame = self.view.frame;
self.scrollView.addSubview(self.imageView)
let customView = CustomView(frame: UIScreen.mainScreen().bounds)
customView.backgroundColor = UIColor.clearColor()
customView.addMovedViewInView()
self.backView.addSubview(customView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
**Demo from github: **
Reference from: