問題はこれです:私NKIngredient
はのサブクラスであるクラスを持っていUIImageView
ます。touchesBegan/Moved/Ended
その中にメソッドを実装し、 userInteractionEnabled
onを設定しましたYES
。しかし、主な問題はNKIngredient
、View Controller でインスタンスをアニメーション化するときに、アニメーション中にオブジェクトに触れることができる必要があることです。そして、これは不可能です!のインターフェースNKIngredient
:
@protocol NKIngredientDelegate <NSObject>
- (void)ingredientTouched;
@end
@interface NKIngredient : UIImageView {
CGPoint touchStart;
}
@property (weak, nonatomic) id <NKIngredientDelegate> delegate;
- (void)animate:(void (^)(void))animationBlock;
@end
実装ファイル:
@implementation NKIngredient
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setUserInteractionEnabled:YES];
}
return self;
}
//Viene chiamato questo metodo se l'oggetto è disegnato come nib
- (id)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"NKIngredient initWithCoder");
if (self = [super initWithCoder:aDecoder]) {
[self setUserInteractionEnabled:YES];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (BOOL)becomeFirstResponder {
return YES;
}
- (void)animate:(void (^)(void))animationBlock {
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionAllowUserInteraction animations:animationBlock completion:^ (BOOL finished) {
NSLog(@"Completed");
}];
}
#pragma mark - Touch interaction
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//NSLog(@"Touches began");
[_delegate ingredientTouched];
touchStart = [[touches anyObject] locationInView:self];
NSLog(@"Touched");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touches moved");
CGPoint point = [[touches anyObject] locationInView:self];
self.center = CGPointMake(self.center.x + point.x - touchStart.x, self.center.y + point.y - touchStart.y);
}
@end
そして、これは私のView Controllerで行うことです:
ingredient = [[NKIngredient alloc] initWithFrame:CGRectMake(20, -50, 34, 45)];
[ingredient setImage:[UIImage imageNamed:@"liv1_Burro.png"]];
[ingredient setUserInteractionEnabled:YES];
[[self view] addSubview:ingredient];
[ingredient animate:^ (void) {
[ingredient setFrame:CGRectMake(20, 200, 34, 45)];
}];
オブジェクトがアニメーション化されている場合でもタッチを取得するための解決策はありますか? NKIngredient
が静止している場合、メソッドtouchesBegan/Moved/Ended
が機能するためです。