0

I'm trying to make a UIScrollView with a button on it. I can make the UIScrollView work, but when enabling a button action, the scroll view freezes at some points. How can I make this work?

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)")

}
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
danielsalare
  • 335
  • 3
  • 14
  • What do you mean by “the scroll view freezes at some points”? – Zev Eisenberg Aug 29 '14 at 19:28
  • If I just open the view, and the first thing I do is scroll the UIScrollView, it scrolls normally, but if after that I tap a button, it gets kind of difficult to make it scroll again. – danielsalare Aug 29 '14 at 19:30
  • Try different permutations of `canCancelContentTouches` and `delaysContentTouches`. You may not need either or both of them. – Zev Eisenberg Aug 29 '14 at 19:38
  • already tried that, but I still can make a smooth scroll, without it freezing – danielsalare Aug 29 '14 at 19:44
  • Is this on a device or in the simulator? I’ve found the iOS 8 simulator to be pretty slow. Also, you could try running your code through the Time Profiler in Instruments to see if anything is CPU bound. – Zev Eisenberg Aug 29 '14 at 19:47
  • Im trying on a device http://youtu.be/JFh0ItGNeR0 – danielsalare Aug 29 '14 at 19:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60261/discussion-between-zev-eisenberg-and-edse). – Zev Eisenberg Aug 29 '14 at 20:19

0 Answers0