附加的代码有两个问题。
首先,如果将形状的动画持续时间设置为1秒,以便形状始终在移动,则touchesBegan不会触发。当我浏览时C4View.m
,似乎UIViewAnimationOptionAllowUserInteraction
没有设置。有没有办法我可以访问UIView
来设置此选项?似乎不重写就很难C4View
。
其次,每次碰到形状时,我都希望它缩小一点。我遇到的问题是,在我第一次击打它之后,它就停止了移动。经过一番挖掘后,看起来似乎C4Shape
在用新框架重建后就失去了形状。这是之前的控制台信息:
[C4Log] <C4Shape: 0x7b63820; baseClass = UIControl;
frame = (263 691; 200 200);
animations = { position=<CABasicAnimation: 0x1082c490>;
animateFillColor=<CABasicAnimation: 0x8a62c00>;
animateLineDashPhase=<CABasicAnimation: 0x8a64920>;
animateStrokeColor=<CABasicAnimation: 0x8a64ef0>;
animateStrokeEnd=<CABasicAnimation: 0x8a65190>;
animateStrokeStart=<CABasicAnimation: 0x8a65430>; };
layer = <C4ShapeLayer: 0x7b63d00>>
之后:
[C4Log] <C4Shape: 0x1082d3a0; baseClass = UIControl;
frame = (204 17; 180 180);
layer = <C4ShapeLayer: 0x1082d4e0>>
首先将形状设置为nil,或者在重新定义之后重做动画似乎并不能解决问题。
码
//
// C4WorkSpace.m
// touchgame
//
// Created by Adam Tindale on 2013-09-28.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace
{
C4Shape * shape;
C4Timer * timer;
C4Label * text;
C4Font * font;
int score;
}
-(void)setup
{
score = 0;
shape = [C4Shape ellipse:CGRectMake(self.canvas.center.x, self.canvas.center.y, 200, 200)];
font = [C4Font fontWithName:@"Chalkduster" size:40];
text = [C4Label labelWithText:@"Score : 0" font:font];
[shape setAnimationDuration:0.05];
timer = [C4Timer automaticTimerWithInterval:1.0 target:self method:@"runaway" repeats:YES];
[self listenFor:@"touchesBegan" fromObject:shape andRunMethod:@"imhit"];
[self.canvas addSubview:text];
[self.canvas addSubview:shape];
}
-(void) runaway
{
[shape setCenter:CGPointMake([C4Math randomIntBetweenA:0 andB:self.canvas.width], [C4Math randomIntBetweenA:0 andB:self.canvas.height])];
C4Log(@"%@",shape);
}
-(void) imhit
{
[text setText:[NSString stringWithFormat:@"Score: %d",++score]];
[text sizeToFit];
CGRect r = shape.frame;
r.size = CGSizeMake(shape.size.width * 0.9, shape.size.height * 0.9);
shape = [C4Shape ellipse:r];
}
@end
C4Control / C4View / C4Window
都有动画选项,允许用户在动画过程中进行交互。这C4AnimationOptions
是可以在中找到的结构的一部分C4Defines.h
。
在开始动画之前,请确保调用此命令。
shape.animationOptions = ALLOWSINTERACTION;
而且,就像其他选项一样,它可以被位掩码:
shape.animationOptions = EASEOUT | AUTOREVERSE | ALLOWSINTERACTION;
(您现在不必重新加工C4View!)
要更改形状的大小,您需要像这样调用形状本身的方法:
[shape ellipse:r];
以下...
shape = [C4Shape ellipse:r];
...由于以下三个原因而无法工作:
C4Shape
您实际上是在创建一个新对象,而不影响已经存在的对象shape = [...];
您可以将指针从原始形状更改为刚创建的新形状以下代码是您上面内容的修改后的版本:
#import "C4WorkSpace.h"
@implementation C4WorkSpace
{
C4Shape * shape;
C4Timer * timer;
C4Label * text;
C4Font * font;
int score;
}
-(void)setup
{
score = 0;
shape = [C4Shape ellipse:CGRectMake(self.canvas.center.x, self.canvas.center.y, 200, 200)];
font = [C4Font fontWithName:@"Chalkduster" size:40];
text = [C4Label labelWithText:@"Score : 0" font:font];
[shape setAnimationDuration:0.05];
timer = [C4Timer automaticTimerWithInterval:1.0 target:self method:@"runaway" repeats:YES];
[self listenFor:@"touchesBegan" fromObject:shape andRunMethod:@"imhit"];
[self.canvas addSubview:text];
[self.canvas addSubview:shape];
}
-(void) runaway {
shape.animationOptions = ALLOWSINTERACTION;
[shape setCenter:CGPointMake([C4Math randomIntBetweenA:0 andB:self.canvas.width], [C4Math randomIntBetweenA:0 andB:self.canvas.height])];
C4Log(@"%@",shape);
}
-(void) imhit
{
[text setText:[NSString stringWithFormat:@"Score: %d",++score]];
[text sizeToFit];
CGRect r = shape.frame;
r.size = CGSizeMake(shape.size.width * 0.9, shape.size.height * 0.9);
[shape ellipse:r];
}
@end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句