1

在 Xcode 中,如何通过触摸我希望它出现的位置来使图像出现在 iPhone 屏幕上的某个位置?我目前尝试使用的方法是获取该点的坐标,然后尝试让 png 文件出现在该点。这是我到目前为止的代码:

- (void) touchesBegan:(NSSet *)touches
            withEvent:(UIEvent *)event {
    UITouch *theTouch = [touches anyObject];
    startPoint = [theTouch locationInView:self.view];
    CGFloat x = startPoint.x;
    CGFloat y = startPoint.y;

但是现在我已经掌握了要点,我不确定如何让图像出现在那里,或者是否有更有效的方法。有任何想法吗?

4

2 回答 2

0

您可以使用[-UIImage drawAtPoint:].

于 2012-10-01T03:45:53.850 回答
0

获取触摸事件的 x,y 坐标后,将 UIImageView 的中心点设置为它。

UIImageView *myImage = ...

CGPoint myPoint = CGPointMake(x_touch, y_touch);

[myImage setCenter:myPoint]; 

您还可以通过在图像视图上使用 animate 块来为这种更改设置动画,如下所示:

    nsTimerInterval myAnimationDuration = 1.0;
    [UIView animateWithDuration:myAnimationDuration animations:^{
        [myImage setCenter:myPoint];

    }
    completion:^(BOOL finished){
        //handle completion tasks

    }];
于 2012-10-01T17:08:58.063 回答