2012-11-05 55 views
0

問題是這樣的:我有NKIngredient這是UIImageView的一個子類。我已經在其中實施了touchesBegan/Moved/Ended方法,並在YES上設置了userInteractionEnabled。但主要問題是,當我在視圖控制器中動畫我的NKIngredient實例時,我需要在動畫期間可以觸摸該對象。這是不可能的! 的NKIngredient接口:UIViewAnimationOptionAllowUserInteraction不起作用

@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 

這是我在我的視圖控制器做:

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方法的作品。

+0

這是什麼版本的iOS上運行? –

+0

你可以嘗試UIGestureRecognizer類。 –

+0

我正在使用iOS 6. UIGestureRecognizer是一個很好的解決方案,但我需要移動該對象。所以,用戶可以拖動對象。是否可以使用UIGestureRecognizer? @Ishank –

回答

1

必須按位或標誌一起,不和......

UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionAllowUserInteraction 

應該

UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionAllowUserInteraction 

否則會相互抵消。此外,我注意到動畫觸摸只註冊動畫將完成的點...

+0

好抓人... –

+0

好的,改了。但情況並未改變。如果我在UIViewController類中使用touchesBegan代碼,它可以工作。但我需要在NKIngredient類,UIImageView的子類中實現它 –