因此,我有一個UIImageView,它具有動畫UIImage。 UIImageView本身使用CAKeyframeAnimation和CGMutablePathRef來動畫,以在路徑中提供一些隨機性。我的問題是我需要跟蹤何時點擊動畫的UIIMageView。如何獲取動畫UIImageView的座標
我已經把頭撞在牆上了一會兒,看起來我應該將UITouch與UIImage的表現層在屏幕上的位置進行比較,但我無法弄清楚如何獲取它的座標。
所以主要的問題是,我如何獲得UIIMageView的座標,並在屏幕上進行動畫並將其與龍頭的出現位置進行比較?
如果我運行的代碼是正確的,現在,它會拋出一個錯誤:
- [CALayer的中心]:無法識別的選擇發送到實例0xa105150
它不再崩潰,但我仍然在對於如何確定UIImageView在屏幕上移動時的位置有所損失。
我的相關代碼如下。任何幫助將不勝感激。
- (void)animateCarrierFish
{
NSInteger getWhichFish = (arc4random()%[self.fishes count]);
NSArray *selectedFish = [NSArray arrayWithArray:[self.fishes objectAtIndex:getWhichFish]];
UIImage *fishName = [selectedFish objectAtIndex:0];
UIImageView *fishCarrier = [[UIImageView alloc] initWithImage:fishName];
fishCarrier.animationImages = selectedFish;
fishCarrier.animationDuration = 1.5;
fishCarrier.animationRepeatCount = 0;
fishCarrier.frame = CGRectMake(0, 0, fishName.size.width*screenScale, fishName.size.height*screenScale);
NSInteger startPoint;
NSInteger endPoint;
NSInteger oppositeMod;
NSInteger fromWhichDirection = (arc4random()%2);
if(fromWhichDirection==1)
{
startPoint = rightEdge;
endPoint = leftEdge;
oppositeMod = -rightEdge;
fishCarrier.transform = CGAffineTransformMakeScale(-1, 1);
} else {
startPoint = leftEdge;
endPoint = rightEdge;
oppositeMod = 0;
}
NSInteger aniSpeed = [self getRandomNumberBetweenMin:15 andMax:20];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = aniSpeed;
pathAnimation.calculationMode = kCAAnimationCubic;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef pointPath = CGPathCreateMutable();
NSInteger yPoint = [self getRandomNumberBetweenMin:waterLine andMax:seaFloor];
CGPathMoveToPoint(pointPath, NULL, startPoint, yPoint);
NSInteger howManyDips = [self getRandomNumberBetweenMin:2 andMax:4];
int i=0;
for (i = 0; i < howManyDips; i++)
{
yPoint = [self getRandomNumberBetweenMin:waterLine andMax:seaFloor];
NSInteger xMod = [self getRandomNumberBetweenMin:-25 andMax:25];
NSInteger xPoint = (([[UIScreen mainScreen] bounds].size.height/(howManyDips+1))*(i+1)) + xMod + oppositeMod;
if (xPoint <0)
{
xPoint = xPoint*-1;
}
CGPathAddLineToPoint(pointPath, NULL, xPoint, yPoint);
}
yPoint = [self getRandomNumberBetweenMin:waterLine andMax:seaFloor];
CGPathAddLineToPoint(pointPath, NULL, endPoint, yPoint);
pathAnimation.path = pointPath;
CGPathRelease(pointPath);
[fishCarrier.layer addAnimation:pathAnimation forKey:@"pathAnimation"];
[self.view addSubview:fishCarrier];
[fishCarrier startAnimating];
fishCarrier.userInteractionEnabled = YES; // these two lines don't seem to do anything
fishCarrier.multipleTouchEnabled = YES;
fishCarrier.tag=[self.swimmers count];
[self.swimmers addObject:fishCarrier];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touch_point = [touch locationInView:self.view];
for (UIImageView *image in self.swimmers) {
// HOW do I get the coordinates in the main view of this sublayer?
}
}
感謝您解決崩潰問題。我正在重複我在這裏其他地方看到的嘗試訪問動畫層的座標。更新了代碼後,我意識到我仍然只接收來自UIImageView的本地座標,而不是它在父視圖中的位置。 有關如何做到這一點的任何提示? – 2013-03-04 19:46:50
@CharlesPayne使用'layer.frame'而不是'layer.bounds',從你的問題中不清楚你想要哪一個。 – 2013-03-04 19:48:33
謝謝! 我試過了,但它輸出的信息與layer.bounds完全相同。不知道我在這裏錯過了什麼。動畫的UIImageView顯然正在移動,但是返回的座標只與自身相關,而不是與父體相關。 – 2013-03-04 20:06:20