如何分层UIButtons和SpriteNodes

时间:2016-06-22 02:32:52

标签: swift uiscrollview sprite-kit uibutton

好的,所以我想知道如何分层UIButtons& SpriteNodes使我的SpriteNodes位于UIButtons的顶部。此外,我的UIButtons位于我创建的滚动视图上。帮助会很好!!

代码:

 let trunk = SKSpriteNode(imageNamed: "Trunk")

    trunk.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
    trunk.zPosition = 55
    self.addChild(trunk)

    let frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
    let scrollView = UIScrollView(frame: frame)
    scrollView.backgroundColor = UIColor.clearColor()
    scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*4)
    scrollView.pagingEnabled = false
    self.view?.addSubview(scrollView)


    let buyButton1 = UIButton(type: .Custom)
    buyButton1.frame = CGRect(x: 10, y: 70, width: UIScreen.mainScreen().bounds.width/5, height: UIScreen.mainScreen().bounds.height/6)

    let image1 = UIImage(named:"Buy999Button")



     buyButton1.setImage(image1, forState: .Normal)
     buyButton1.addTarget(self, action: "something", forControlEvents: .TouchUpInside)
    buyButton1.layer.zPosition = 1
    scrollView.addSubview(buyButton1)
    scrollView.layer.zPosition = 1
    scrollView.sendSubviewToBack(scrollView)

1 个答案:

答案 0 :(得分:0)

SKView本身就是一个UIView。所有SpriteKit节点都是SKView的成员并包含在其中。您只能更改视图的绘制顺序,而不能更改其内容。

SpriteKit节点不是开始的视图,而是绘制到由SKView管理的OpenGL帧缓冲区上。从Cocoa的角度来看,SKView是一个没有子视图的单一视图。

简单地说:节点在技术上与视图不兼容。

注意: 您可以使用UITapGestureRecognizer使Sprite节点像按钮一样工作。

类似的东西:

if (gestureRecognizer == self.tapGesture) {
    return CGRectContainsPoint(self.neededSprite.frame, [self convertPoint:[sender locationInView:self.view] toNode:self]);
}
return YES;
}