簡體   English   中英

為UIImageview設置Uianimation和Touch事件

[英]To set Uianimation and Touch Event for UIImageview

我在一個項目中工作,需要將圖像從一個地方動畫化到另一個地方,我完成了,但是我的問題是在動畫時我沒有從該UIImageview獲得觸摸事件。所以任何知道的請盡快給出解決方案。

- (void) imageSpawn
  {

NSArray *images=  [NSArray arrayWithObjects:[UIImage imageNamed:@"fish_right1.png"],
                   [UIImage imageNamed:@"fish_right2.png"], [UIImage imageNamed:@"fish_right3.png"], [UIImage imageNamed:@"fish_right4.png"], [UIImage imageNamed:@"fish_right14.png"], [UIImage imageNamed:@"fish_right15.png"], [UIImage imageNamed:@"fish_right20.png"], nil];
int currentImageIndex=0;

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [self.first_fish setImage:[images objectAtIndex:currentImageIndex] ];
}completion:Nil ];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
  }

  -(void)ballTapped:(UIGestureRecognizer *)gesture
   {
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
NSLog(@"LOCA X:%d",gesture.view.tag);
 NSLog(@"LOCA y:%f",location.y);

        }

此致Raja.I

有一個選項UIViewAnimationOptionAllowUserInteraction,您可以將其傳遞給animateWithDuration:delay:options:animations:completion:中的options參數。 您需要設置它以允許在動畫期間進行交互。

編輯后:這與制作動畫的方式有關。 如果單擊圖像視圖結束的位置(在動畫運行時),您將看到手勢識別器將觸發。 實際上,動畫開始時,視圖的位置已經設​​置為最終值。

為了使此工作有效,我認為您必須使用計時器而不是animateWithDuration。 創建一個重復計時器,並在每次調用時增加視圖的x位置,並在到達目的地時使計時器無效。 這將允許您在視圖移動時與其進行交互。

為視圖設置動畫時,將視圖設置為最終狀態。 這樣,您只能在動畫制作的最終位置檢測到觸摸。

但是,有可能解決該問題。 您需要捕獲觸摸事件並將其與presentationLayer進行比較。

-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent *) event {

    CGPoint point = [[touches anyObject] locationInView:self.view];

    if([self.cloudAnimate.layer.presentationLayer hitTest:point]) {
        //do something
    }
}

表示層具有與您的cloudAnimate關聯的CALayer 視覺位置的信息。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if([imageView1.layer.presentationLayer hitTest:touchLocation]){
       // TODO
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM