UIScrollView点击按钮不起作用

时间:2016-06-14 20:50:56

标签: swift uiscrollview sprite-kit

我有一个我正在创建的游戏,在我的商店里我想使用像游戏Color Switch这样的滚动视图。但是,当我点击按钮时,它们将无法运行,因为滚动视图不能像那样工作。谁能帮我解决这个问题?

我按照本教程制作滚动视图&在我的项目中有帮助:https://github.com/crashoverride777/Swift-SpriteKit-UIScrollView-Helper

这是我的自定义滚动视图:

 /// Scroll direction
 enum ScrollDirection: Int {
case Vertical
case Horizontal
 }

 /// Custom UIScrollView class
class CustomScrollView: UIScrollView {

// MARK: - Static Properties

/// Touches allowed
static var disabledTouches = false

/// Scroll view
private static var scrollView: UIScrollView!

// MARK: - Properties

/// Nodes touched. This will forward touches to node subclasses.
private var nodesTouched: [AnyObject] = []

/// Current scene
private let currentScene: SKScene

/// Moveable node
private let moveableNode: SKNode

/// Scroll direction
private var scrollDirection: ScrollDirection

// MARK: - Deinit
  deinit {
    print("Custom scroll view deinit")
  }

 // MARK: - Init
init(frame: CGRect, scene: SKScene, moveableNode: SKNode, scrollDirection: ScrollDirection) {
    self.currentScene = scene
    self.moveableNode = moveableNode
    self.scrollDirection = scrollDirection
    super.init(frame: frame)

    CustomScrollView.scrollView = self
    self.frame = frame
    indicatorStyle = .White
    scrollEnabled = true
    //self.minimumZoomScale = 1
    //self.maximumZoomScale = 3
    canCancelContentTouches = false
    userInteractionEnabled = true
    delegate = self

    clipsToBounds = true

    //contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 50, right: 0)

    // Flip for spritekit (only needed for horizontal)
    if self.scrollDirection == .Horizontal {
        let flip = CGAffineTransformMakeScale(-1,-1)
        self.transform = flip
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
   }

  // MARK: - Touches
  extension CustomScrollView {

/// Began
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //super.touchesBegan(touches, withEvent: event)

    for touch in touches {
        let location = touch.locationInNode(currentScene)

        guard !CustomScrollView.disabledTouches else { return }

        /// Call touches began in current scene
        currentScene.touchesBegan(touches, withEvent: event)

        /// Call touches began in all touched nodes in the current scene
        nodesTouched = currentScene.nodesAtPoint(location)
        for node in nodesTouched {
            node.touchesBegan(touches, withEvent: event)



        }
    }
   }

/// Moved
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //super.touchesMoved(touches, withEvent: event)

    for touch in touches {
        let location = touch.locationInNode(currentScene)

        guard !CustomScrollView.disabledTouches else { return }

        /// Call touches moved in current scene
        currentScene.touchesMoved(touches, withEvent: event)

        /// Call touches moved in all touched nodes in the current scene
        nodesTouched = currentScene.nodesAtPoint(location)
        for node in nodesTouched {
            node.touchesMoved(touches, withEvent: event)
        }
    }
}

/// Ended
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //super.touchesEnded(touches, withEvent: event)

    for touch in touches {
        let location = touch.locationInNode(currentScene)

        guard !CustomScrollView.disabledTouches else { return }

        /// Call touches ended in current scene
        currentScene.touchesEnded(touches, withEvent: event)













        /// Call touches ended in all touched nodes in the current scene
        nodesTouched = currentScene.nodesAtPoint(location)
        for node in nodesTouched {
            node.touchesEnded(touches, withEvent: event)





        }
    }
}

/// Cancelled
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    //super.touchesCancelled(touches, withEvent: event)

    for touch in touches! {
        let location = touch.locationInNode(currentScene)

        guard !CustomScrollView.disabledTouches else { return }

        /// Call touches cancelled in current scene
        currentScene.touchesCancelled(touches, withEvent: event)

        /// Call touches cancelled in all touched nodes in the current scene
        nodesTouched = currentScene.nodesAtPoint(location)
        for node in nodesTouched {
            node.touchesCancelled(touches, withEvent: event)
        }
    }
}
  }

 // MARK: - Touch Controls
  extension CustomScrollView {

/// Disable
class func disable() {
    CustomScrollView.scrollView?.userInteractionEnabled = false
    CustomScrollView.disabledTouches = true
}

/// Enable
class func enable() {
    CustomScrollView.scrollView?.userInteractionEnabled = true
    CustomScrollView.disabledTouches = false
}
 }

  // MARK: - Delegates
  extension CustomScrollView: UIScrollViewDelegate {

func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollDirection == .Horizontal {
        moveableNode.position.x = scrollView.contentOffset.x
    } else {
        moveableNode.position.y = scrollView.contentOffset.y
    }
}
  }

这是我的moveableScrollNode,我创建的按钮节点&amp;触摸功能:

let moveableNode = SKNode()
 weak var scrollView: CustomScrollView!










override func didMoveToView(view: SKView) {

    addChild(moveableNode)




    scrollView = CustomScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), scene: self, moveableNode: moveableNode, scrollDirection: .Vertical)
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 3)// makes it 3 times the height
    moveableNode.zPosition = 0
    view.addSubview(scrollView)





    let page1ScrollView = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height))
    page1ScrollView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    moveableNode.addChild(page1ScrollView)

    let page2ScrollView = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height))
    page2ScrollView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.frame.size.height))
    moveableNode.addChild(page2ScrollView)

    let page3ScrollView = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height))
    page3ScrollView.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.frame.size.height * 2))
    moveableNode.addChild(page3ScrollView)








let sprite1Page1 = SKSpriteNode(imageNamed: "Buy5Button")
     sprite1Page1.position = CGPointMake(self.frame.size.width * -0.4, self.frame.size.height*0.05)
         page1ScrollView.addChild(sprite1Page1)
 }

 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if sprite1Page1.containsPoint(location) {

            for touch: AnyObject in touches {
                _ = touch.locationInNode(self)

                let transition = SKTransition.fadeWithColor(SKColor.blackColor(), duration: 1)

                let newScene = GameScene(size: self.size)
                newScene.scaleMode = scaleMode
                self.view?.presentScene(newScene, transition: transition)

            }

        }

    }

}

1 个答案:

答案 0 :(得分:1)

尝试将if OBJECT_ID('tempdb..#tmp') is not null drop table #tmp if OBJECT_ID('tempdb..#tmpsplit') is not null drop table #tmpsplit declare @cols nvarchar(MAX), @sql nvarchar(MAX) create table #tmp (id int identity(1,1), activities varchar(255)) insert into #tmp (activities) values ('yoga, hiking'), ('art, music, yoga') /* split activities into individual rows and store in #tmpsplit*/ SELECT DISTINCT A.ID, ltrim(rtrim(Split.a.value('.', 'VARCHAR(max)'))) AS activity into #tmpsplit FROM (SELECT id, CAST ('<M>' + REPLACE(CAST(activities AS VARCHAR), ',', '</M><M>') + '</M>' AS XML) AS String FROM #tmp) AS A CROSS APPLY String.nodes ('/M') AS Split(a) WHERE LEN(Split.a.value('.', 'VARCHAR(max)'))>1 /* create list of activities to pivot */ SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.activity) FROM #tmpsplit c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'') /* create pivot: id and columns only*/ set @sql = 'select id, ' + @cols + ' from (select s.*, ''y'' as val from #tmpsplit s) datatable pivot (max(val) for activity in (' + @cols + ') ) p ' print(@sql) exec(@sql) /* create pivot: id, full activitites string, and columns*/ set @sql = 'select id, activities, ' + @cols + ' from (select s.*, t.activities, ''y'' as val from #tmpsplit s join #tmp t on s.id=t.id) datatable pivot (max(val) for activity in (' + @cols + ') ) p ' print(@sql) exec(@sql) UIScrollView

一起使用
UIButtons