UIButton
内的{p> scrollview
未触发事件。我尝试设置exclusiveTouch delaysTouchesEvent
,但没有任何帮助。
class TestViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor()]
override func viewDidLoad() {
super.viewDidLoad()
let scrollingView = colorButtonsView(CGSizeMake(150,scrollView.frame.size.height), buttonCount: 5)
scrollView.contentSize = scrollingView.frame.size
scrollView.addSubview(scrollingView)
scrollView.showsVerticalScrollIndicator = false
scrollView.delegate = self
scrollView.pagingEnabled = true
scrollView.indicatorStyle = .Default
scrollView.canCancelContentTouches = false
scrollView.delaysContentTouches = false
scrollView.exclusiveTouch = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func colorButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView {
let buttonView = UIView()
buttonView.frame.origin = CGPointMake(0,0)
let padding = CGSizeMake(10, 10)
buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(buttonCount)
var buttonPosition = CGPointMake(padding.width * 0.5, padding.height)
let buttonIncrement = buttonSize.width + padding.width
for i in 0...(buttonCount - 1) {
var button = UIButton.buttonWithType(.Custom) as! UIButton
button.frame.size = buttonSize
button.frame.origin = buttonPosition
buttonPosition.x = buttonPosition.x + buttonIncrement
button.setTitle(categoryArr[i], forState: UIControlState.Normal)
button.addTarget(self, action: "showTheNextButton:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = buttonColors[i]
buttonView.addSubview(button)
}
return buttonView
}
func showTheNextButton(sender:UIButton){
print("ok show the next button")
}
}
extension TestViewController:UIScrollViewDelegate{
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let index = round(scrollView.contentOffset.x / scrollView.frame.size.width)
print(index)
}
}
答案 0 :(得分:1)
如果UIButton
位于其超级视图的范围之外,则{{1}}将不接受触摸。确保所有视图都是您期望的大小和位置
答案 1 :(得分:1)
请进行以下更改:
self.scrollView.canCancelContentTouches = true
self.scrollView.delaysContentTouches = true
在for
循环中,将let buttonView = UIView()
移至:
@IBOutlet weak var scrollView: UIScrollView!
var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor()]
let buttonView = UIView()
然后,再次在for
循环中更改以下内容:
更改此行:buttonView.frame.origin = CGPointMake(0,0)
至
self.buttonView.frame = self.scrollView.bounds
self.buttonView.userInteractionEnabled = true
<强> 附录: 强>
对此进行评论://self.scrollView.exclusiveTouch = true
我冒昧地测试它;触摸应该在按钮上注册,滚动也应该水平工作。享受。