Ios 如何在点击时显示字符串?
假设我有一根绳子和一个标签Ios 如何在点击时显示字符串?,ios,objective-c,nsstring,sprite-kit,Ios,Objective C,Nsstring,Sprite Kit,假设我有一根绳子和一个标签 NSString *testString = @"Hello world, this is a test"; SKLableNode *label; label.text = testString; 如何使标签每次点击时都显示更多的testString?例如,标签一开始是空的,点击屏幕时会显示以下内容 第一次点击:“你好,世界,” 第二次点击:“你好,世界,这里是” 第三次点击:“你好,世界,这是一个测试”一种可能是在SKLabelNode上方添加一个视图。然后将手
NSString *testString = @"Hello world, this is a test";
SKLableNode *label;
label.text = testString;
如何使标签每次点击时都显示更多的testString?例如,标签一开始是空的,点击屏幕时会显示以下内容
第一次点击:“你好,世界,”第二次点击:“你好,世界,这里是”
第三次点击:“你好,世界,这是一个测试”一种可能是在
SKLabelNode
上方添加一个视图。然后将手势识别器添加到标签上,并在点击时更改其文本属性。没有真正的方法。我唯一能想到的是有3个不同的字符串,每次有人点击按钮,它就会显示一个不同的字符串。像这样
var次数
var string1=“你好,世界”
var string2=“你好,就是世界”
var string3=“hello world,这是一个测试”您可以采取几种方法来实现这一点。您可以创建一个对象来保存有问题的testString
,以及曝光量。其中“曝光量”表示要显示的字符串数量。这可以是百分比、字符数等。如果是百分比,请记住按实际字符数四舍五入
轻触后,更新曝光量。这反过来会更新SKLabel
的文本字段
另一种方法是使用面具。如果您不在乎是否部分显示了角色的一部分,这将非常有用。你仍然需要管理“曝光量”
示例类来执行此操作
@interface ExposedText : NSObject
@property (nonatomic, copy) NSString *fullString;
@property (nonatomic, copy) NSString *exposedString; // This will indicate the exposed string
@property (nonatomic, assign) float amountExposed; // This is to set your exposure amount, in this case it's a %
@property (nonatomic, strong) SKLabel *label; // This is the reference to the label this object will modify, IF you want this object to update the label directly
- (void)increaseExposure:(float)increaseAmt; // Example method to increase the amount, likewise, you could have a customer getter for amountExposed and just use that
@end
我还没有定义init
变量是什么样子的
要点是这个类知道如何“公开”适当数量的字符串。你必须确定这个的算法fullString
将是您的完整字符串。在您的案例中,“你好,这是一个测试”
。如果假设exposureAmt为0.5(50%),则exposedString
将返回“Hello world”
。您可以让这个类直接更新SKLabel
(这就是我将它作为属性的原因)。或者,您可以让其他代码使用该类的实例并更新SKLabel
注意:如果您选择掩码路由(SKCropNode
),并且希望类更新它,则必须添加相应的属性,等等。使用(NSString*)substringWithRange:(NSRange)aRange。使用整数作为计数器来更新范围。