如何获取动画UIImageView的坐标
问题描述:
因此,我有一个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?
}
}
答
我能够最终得到的对象报告,他们使用了如下修改我的touchesBegan方法是在屏幕上:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint startPoint = [touch locationInView:self.view];
CGFloat x = startPoint.x;
CGFloat y = startPoint.y;
//create touch point
CGPoint touchPoint = CGPointMake(x, y);
// loop through the items we're iterested in being tapped on here
for (UIImageView *fish in self.swimmers) {
CGRect fishFrame = [[fish.layer presentationLayer] frame];
if (CGRectContainsPoint(fishFrame, touchPoint)) {
NSLog(@"You hit fish: %u",fish.tag); // do stuff here
}
}
}
答
你为什么想到CALayer
向center
消息作出响应?如果您已阅读其documentation,则会发现它没有实施名为center
的方法。如果你想获得该层的几何中心,手动计算的话:
CGPoint center;
center.x = CGRectGetMidX(layer.bounds);
center.y = CGRectGetMidY(layer.bounds);
编辑:看来你要相对于超级中锋(层查看什么...?):
CGPoint center;
center.x = CGRectGetMidX(layer.frame);
center.y = CGRectGetMidY(layer.frame);
感谢您解决崩溃问题。我正在重复我在这里其他地方看到的尝试访问动画层的坐标。更新了代码后,我意识到我仍然只接收来自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