gpt4 book ai didi

iphone - touchesBegan 在动画期间不触发,C4Shape 在调整大小后失去引用

转载 作者:行者123 更新时间:2023-11-29 10:54:15 24 4
gpt4 key购买 nike

附加代码有两个问题。

首先,如果我将形状的动画持续时间设置为 1 秒,以便形状始终移动,则 touchesBegan 不会触发。当我查看 C4View.m 时,似乎未设置 UIViewAnimationOptionAllowUserInteraction。有没有办法让我可以访问 UIView 来设置这个选项?看来不重写C4View会很困难。

http://iphonedevsdk.com/forum/iphone-sdk-development/64569-uiview-animatewithduration-blocks-the-screen-for-touch-input.html

其次,每次点击形状时我都希望它缩小一点。我遇到的问题是,在我第一次击中它后它停止移动。经过一些挖掘后,似乎 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>>

首先将 shape 设置为 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 |自动反转 |允许交互;

(您不必返工 C4View!)

改变形状大小

要更改形状的大小,您需要像这样调用形状本身的方法:

[shape ellipse:r];

以下...

shape = [C4Shape 椭圆:r];

...由于 3 个原因无法工作:

  1. 通过调用C4Shape,您实际上是在创建一个新对象,不会影响已经存在的对象
  2. 通过设置 shape = [...];,您可以将指针从原始形状更改为刚创建的新形状
  3. 屏幕上不存在新形状,停止的形状是您添加为 subview 的原始形状。这就是您的原始形状停止移动的原因,因为您现在正在引用一个新对象。

以下代码是我对您上面的代码的修改版本:

#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

关于iphone - touchesBegan 在动画期间不触发,C4Shape 在调整大小后失去引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19094936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com