因此,我希望在我的应用程序中有轻击手势识别器,用户不必滚动屏幕,我希望手势识别器只在应用程序的一侧,如30左右。
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)
}
}
}
发布于 2018-06-04 19:01:34
有几种方法可以做到这一点。其中一种简单的方法是在z顺序的顶部添加不可见的视图,并将轻击手势识别器附加到该视图上。支持动态方向更改的AutoLayout组件:
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")
}
}
发布于 2018-06-04 09:43:00
您需要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
https://stackoverflow.com/questions/50671615
复制相似问题