Objective c UITap手势仅在重新加载UIViewController后工作
我在Objective-C工作了一年,这是我提出的第一个问题,因为我总是在这个网站上找到答案,但这种情况完全是疯狂的,没有发现任何类似的问题 我有一个自定义类(从UIView称为BallClass)。UIView内部有一个UIImageView和一个UIApsignature来执行操作 因此,当我在UIViewController中创建对象时,当按下对象时,不会执行tapAction,但如果我切换到其他屏幕并返回它,TapSignature会完美工作 我尝试了很多选择,但我不能在第一次加载工作 我使用ARC、Xcode(4.6.3)、OSX(10.8.4)和IB,但创建这个对象时没有IB 提前谢谢 这是棒球课Objective c UITap手势仅在重新加载UIViewController后工作,objective-c,uitapgesturerecognizer,Objective C,Uitapgesturerecognizer,我在Objective-C工作了一年,这是我提出的第一个问题,因为我总是在这个网站上找到答案,但这种情况完全是疯狂的,没有发现任何类似的问题 我有一个自定义类(从UIView称为BallClass)。UIView内部有一个UIImageView和一个UIApsignature来执行操作 因此,当我在UIViewController中创建对象时,当按下对象时,不会执行tapAction,但如果我切换到其他屏幕并返回它,TapSignature会完美工作 我尝试了很多选择,但我不能在第一次加载工作
- (void)tapGesture:(UIGestureRecognizer *)tapGesture {
NSLog(@"hola");
}
- (id)initBall:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
// Recibir las variables iniciales para el control del timer
ball = [[Ball alloc] init];
// Agregar la imagen de la pelota en el view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
[imageView setCenter:CGPointMake(CGRectGetMidX([self bounds]), CGRectGetMidY([self bounds]))];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:@"pelota.png"]];
[imageView setTag:100];
[imageView setNeedsDisplay];
// Add TAP
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
// Tap Properties
[tapGesture setDelaysTouchesBegan:YES];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture setDelegate:(id)self];
[imageView addGestureRecognizer:tapGesture];
[self addSubview:imageView];
[self setNeedsDisplay];
// Propiedades del View
[self setUserInteractionEnabled:YES];
self.backgroundColor = [UIColor clearColor];
[self setTag:100];
// BALL BOUNCE
[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
}
return self;
}
- (void)onTimer:(NSTimer *)timerLocal {
// Do something
}
... // more methods
以下是示例:
ballView00 = [[BallView alloc] initBall:CGRectMake(10, 10, 40, 40)];
UiView显示完美,动画工作正常,但正如前面提到的那样,在重新加载ViewController之后,UITap手势才工作
下面是一个链接,用于对球和UIView(@kBall)进行镜像。很抱歉,关于图像,但我只有10点才能加载:(这可能是superview的框架存在问题。哪个视图容纳ballView00?ballView是否完全在其superview的范围内 通常,当您遇到问题时,您希望触摸,但没有得到触摸,这是因为接收触摸的视图集超出了其父视图的范围 因此,如果superview边界为100x100,且球位于x=125 y=125,则球将不会受到触碰 当您的视图布局时,会发生很多奇妙的事情,因为一些不正确的约束,有时会发现一个具有0x0维度的视图也就不足为奇了 通过给所有视图添加背景色,您可以轻松检查它们的边界。我找到了解决方案 球用这个旋转:
[UIView animateWithDuration: 0.5f
delay: 0.0f
options: UIViewAnimationOptionCurveEaseIn
animations: ^{
self.transform = CGAffineTransformRotate(self.transform, M_PI / 2);
}
completion: ^(BOOL finished) {
if (finished) {
if (animating) {
// if flag still set, keep spinning with constant speed
[self spinWithOptions: UIViewAnimationOptionCurveLinear];
}
}
}];
我使用简单的add self.transform=cgaffinetTransformRotate(self.transform,M_PI/2);在类中的内部drawRect方法更改旋转操作
显然,我不能嵌套两个计时器,因为“禁用”了点击手势,所以当我改变屏幕并返回时,旋转停止,这有助于发现这种情况。
谢谢您的帮助。Good idea@kball,让我检查一下,稍后返回结果。不幸的是,仍然存在相同的问题。我重新配置了ImageView,但不起作用。实际上,我更新了上面的UIImageView配置代码,并添加了黑色背景的球和UIView的图像。