【问题标题】:SpriteKit -- Change SKShapeNode SizeSpriteKit -- 更改 SKShapeNode 大小
【发布时间】:2015-03-31 17:32:26
【问题描述】:

我有一个由shapeNodeWithRect: cornerRadius: 创建的SKShapeNode。这个圆角矩形是 SKEffectNode 的子对象,因此我可以设置 shouldRasterize = YES

我想根据用户的触摸来改变这个节点的宽度。即,当他们将手指水平向右移动时,矩形会变大(向左移动时会变小)。

  1. 我可以通过用新尺寸的新SKShapeNode 替换原来的SKShapeNode 来做到这一点(但这很糟糕)。
  2. 我尝试在SKEffectNodeSKShapeNode 上运行调整大小操作(但这不起作用,因为调整大小仅适用于SKSpriteNotes [SKAction Apple Docs -- Resize]):

[self runAction:[SKAction resizeToWidth:newSize.width height:newSize.height duration:0]]; [self.shapeNode runAction:[SKAction resizeToWidth:newSize.width height:newSize.height duration:0]];

  1. 我可以像在这个答案中那样更改 xScale:Change height of an SKShapeNode。但如果我这样做,SKShapeNode 就会被像素化。

我应该怎么做?

在 UIKit 中就是这么简单(只需设置一个 UIView 的框架)...

【问题讨论】:

  • 您尝试过扩展吗?
  • @sangony -- 我尝试过扩展(请参阅我的问题中的#3)。问题在于,将节点从小到大缩放会使其像素化。
  • 如果像素化是一个问题,那么您几乎无能为力。我最近发布了一个调整 SKLabelNode 大小的答案,它处理了非常相似的问题。 stackoverflow.com/questions/29354494/…
  • 1.每次通过都重新绘制它有什么问题?它可能很密集,但如果它有效... 2. 如果在拉伸时像素化,请将形状创建为您需要的最宽 x 并立即将其缩小。然后,当他们触摸屏幕时,将比例增加到接近 1。按比例缩小会导致最小的质量损失。放大像素。只需反转您的过程。
  • @meisenman -- 1. 每次通过时重新创建的问题是动画变化。 2. 同意这个作为可能的解决方案(尽管它在我的具体实现中效果不佳,因为我在屏幕上有很多这些 SKShapeNodes 并且担心缩小这么多对象)。

标签: ios sprite-kit cgrect skshapenode


【解决方案1】:

创建具有大尺寸的SKShapeNode 对象,然后立即将它们缩小。如果你这样做,你不应该遇到模糊或像素化的节点。

在 Swift 4.0 中:

class GameScene: SKScene {
    var roundedRect: SKShapeNode!
    didMove(to view: SKView) {
         roundedRect = SKShapeNode(rect: Constants.RoundedRect.largeInitialRect, cornerRadius: Constants.Rect.largeInitialCornerRadius)
         // configure roundedRect
         addChild(roundedRect)
         scaleRoundedRect
    }

    func scaleRoundedRect(to size: CGSize) {
        roundedRect.xScale = roundedRect.xScale / frame.width * size.width
        roundedRect.yScale = roundedRect.yScale / frame.height * size.height
    }
}

在 Objective-C 中:

@implementation GameScene

    - (void) didMoveToView:(SKView *)view {
        CGRect largeRect = CGRectMake(0, 0, 0, 0); // replace with your own values
        CGFloat largeCornerRadius = (CGFloat) 0; // replace with your value
        _roundedRect = [SKShapeNode shapeNodeWithRect:largeRect cornerRadius:largeCornerRadius];
        CGSize initialSize = CGSizeMake(0, 0); // replace with your value
        [self scaleRoundedRectToSize:initialSize];
    }

    - (void) scaleRoundedRectToSize:(CGSize)size {
        _roundedRect.xScale = _roundedRect.xScale / _roundedRect.frame.size.width * size.width;
        _roundedRect.yScale = _roundedRect.yScale / _roundedRect.frame.size.height * size.height;
    }


@end

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多