Swift UIScrollView无法使用按钮

时间:2014-08-29 19:15:23

标签: ios uiscrollview swift ios8

我尝试使用按钮制作UIScrollView。我可以使UIScrollView工作,但是当启用按钮操作时,滚动视图会在某些点冻结。我怎样才能做到这一点?

func setupScrollView() {
    self.scrollView.frame = CGRect(x:0,y:300,width:320, height:menuItemHeight)
    self.scrollView.backgroundColor = scrollBg
    self.scrollView.scrollEnabled = true
    self.scrollView.canCancelContentTouches = true
    self.scrollView.delaysContentTouches = true
    self.scrollView.contentSize = CGSize(width:menuItems.count*menuItemWidth, height:menuItemHeight)
    //scrollView.contentOffset = CGPoint(x: 10, y: 20)

    sourceView.addSubview(scrollView)
    //println("Menu Items: \(menuItems.count)")
    //println("ScrollView Width: \(menuItems.count*menuItemWidth)")

}
func configureContainer(){


    var menupos = 0
    for (index, element) in enumerate(menuItems) {
        //println("Item \(index): \(element)")
        var fieldImageView = UIButton(frame: CGRect(x: menupos, y: 0, width: menuItemWidth, height: menuItemHeight))
        var fieldImage = UIImage(named:element)
        fieldImageView.imageView.image = fieldImage
        fieldImageView.setImage(fieldImage, forState:UIControlState.Normal)
        //fieldImageView.setTitle("Item \(index)", forState: UIControlState.Normal)
        fieldImageView.backgroundColor = UIColor.greenColor()
        fieldImageView.userInteractionEnabled = true
        fieldImageView.tag = index;
        fieldImageView.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchDown)

       // scrollView.touchesShouldCancelInContentView(fieldImageView)
        self.scrollView.addSubview(fieldImageView)

        menupos+=menuItemWidth
    }
}

func buttonAction(sender:UIButton!){
    var btnsendtag:UIButton = sender
    println("Item \(btnsendtag.tag)")

}

0 个答案:

没有答案