iOS 為移動動畫中的View添加touch事件

來源:互聯網
上載者:User

標籤:ios動畫   移動動畫   coreanimation   cakeyframeanimation   cabasicanimation   

對Core Animation來說,不管是顯式動畫還是隱式動畫,對其設定frame都是立即設定的,比如說給一個UIView做移動動畫,雖然看起來frame在持續改變,但其時它的frame已經是最終值了,這種情況下,哪怕這個UIView是UIButton的執行個體,其觸發touch事件的範圍還是最終frame的地方。比如一個Button的frame是(0,0,100,100),要把它從0,0移動到200,200,在這種情況下:
  1. 如果你使用的是顯式動畫(CAKeyframeAnimation和CABasicAnimation),是通過指定path或values來進行動畫的,它的frame並沒有改變,touch範圍還是(0,0,100,100)這個範圍內
  2. 如果你使用的是隱式動畫,是通過設定frame來進行動畫的,那麼它的touch範圍就是(200,200,100,100)這個範圍內
如果我們要為移動動畫中的View添加touch事件,就需要在後面用到hitTest。我們先建立一個動畫Layer:
CGSize layerSize = CGSizeMake(100, 100);CALayer *movingLayer = [CALayer layer];movingLayer.bounds = CGRectMake(0, 0, layerSize.width, layerSize.height);[movingLayer setBackgroundColor:[UIColor orangeColor].CGColor];movingLayer.anchorPoint = CGPointMake(0, 0);[self.view.layer addSublayer:movingLayer];self.movingLayer = movingLayer;
這裡面只有anchorPoint重要一些,因為anchorPoint能影響position的取值,對Layer來說,frame是抽象的,只有bounds和position是真實存在的,並且設定frame和設定anchorPoint的順序不同,開始看到的結果也不同:
先設定anchorPoint開始就能得到正確的結果,即position為0,0,如果設定bounds的話就沒那麼麻煩,順序就無所謂了。之所以要設定anchorPoint,是因為後面的CAKeyframeAnimation將用position來做動畫,如果anchorPoint為預設值(即0.5,0.5)的話,將會出現一個x、y軸一半的位移,一張圖解釋anchorPoint的取值:
position是以anchorPoint為原點的,預設這個原點在中心位置,自然就會出現x、y軸的位移,其實只要我在動畫中將position做個位移就不用設定anchorPoint,但是我覺得動畫從0到終點更直觀一些,所以這才是設定anchorPoint的原因。最後用一個weak屬性引用動畫Layer並把這個Layer添加到self.view.layer中。接下來是動畫部分:
CAKeyframeAnimation *moveLayerAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];//moveLayerAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];//moveLayerAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(320 - self.movingLayer.bounds.size.width, 0)];moveLayerAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(0, 0)],                              [NSValue valueWithCGPoint:CGPointMake(320 - self.movingLayer.bounds.size.width, 0)]];moveLayerAnimation.duration = 2.0;moveLayerAnimation.autoreverses = YES;moveLayerAnimation.repeatCount = INFINITY;moveLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];[self.movingLayer addAnimation:moveLayerAnimation forKey:@"move"];
如果是用CABasicAnimation做動畫,則用fromValue及toValue替換setValues,timingFunction直接用線性,不用做其他變換,關於這個屬性的預置值,我在另一篇博文中有提到。
接下來為self.view添加手勢識別:
........    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];    [self.view addGestureRecognizer:self.tapGesture];}-(void)click:(UITapGestureRecognizer *)tapGesture {    CGPoint touchPoint = [tapGesture locationInView:self.view];    if ([self.movingLayer.presentationLayer hitTest:touchPoint]) {        NSLog(@"presentationLayer");    }}
我在最開始的時候有提到,動畫的過程只是看起來是動態變換的,其內部的值已經是固定的了,在這種情況下,Layer內部會通過複製一個當前Layer的副本來展示動畫過程,而我們可以通過訪問Layer的presentationLayer屬性來得到這個副本的副本,通過presentationLayer我們可以知道當前動畫已經進行到了螢幕的哪個位置上了,再直接通過hitTest來判定是不是一次有效點擊即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.