Reputation: 15
So I want to have the tap gesture recognizer in my app where the user doesn't have to scroll the screen I want the gesture recognizer only be on the side of the app like 30 from the right and the left.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var images = [UIImageView]()
var contentWidth: CGFloat = 0.0
var contentAdded = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !contentAdded {
contentAdded = true
for x in 0...2 {
let image = UIImage(named: "icon\(x).png")
let imageView = UIImageView(image: image)
images.append(imageView)
var newX: CGFloat = 0.0
newX = view.frame.midX + view.frame.size.width * CGFloat(x)
contentWidth += view.frame.size.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: newX - 75, y: (view.frame.size.height / 2) - 75, width: 150, height: 150)
}
scrollView.contentSize = CGSize(width: contentWidth, height: view.frame.size.height)
}
}
}
Upvotes: 1
Views: 279
Reputation: 758
There are several ways to do this. One of the simple ways is to add invisible views on top of the z-order and attach tap gesture recogniser to the same. AutoLayout stuff to support dynamic orientation changes:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Call this at the end to make sure our Left/Right taps are on top of z-index
setupLeftRightTaps()
}
func setupLeftRightTaps() {
let leftView = UIView()
// leftView.backgroundColor = .yellow
leftView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(leftTap)))
view.addSubview(leftView)
let rightView = UIView()
// rightView.backgroundColor = .red
rightView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(rightTap)))
view.addSubview(rightView)
// Layout
rightView.translatesAutoresizingMaskIntoConstraints = false
leftView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
leftView.topAnchor.constraint(equalTo: view.topAnchor),
leftView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
leftView.widthAnchor.constraint(equalToConstant: 30),
rightView.topAnchor.constraint(equalTo: view.topAnchor),
rightView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
rightView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
rightView.widthAnchor.constraint(equalToConstant: 30)
])
}
@objc func leftTap(_ sender: UITapGestureRecognizer) {
print("Left Tapped")
}
@objc func rightTap(_ sender: UITapGestureRecognizer) {
print("Right Tapped")
}
}
Upvotes: 0
Reputation: 11
you need UIScreenEdgePanGestureRecognizer
/*! This subclass of UIPanGestureRecognizer only recognizes if the user slides their finger
in from the bezel on the specified edge. */
NS_CLASS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED @interface UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer
@property (readwrite, nonatomic, assign) UIRectEdge edges; //< The edges on which this gesture recognizes, relative to the current interface orientation
@end
Upvotes: 1