0

Xcodeは、ユーザーが触れる場所に画像を配置します

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:touch.view];

UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"BluePin.png"];
[image drawAtPoint:point];
}

基本的にタッチスクリーンの画像はタッチした場所に表示されるはずですが、何も表示されません...

4

2 に答える 2

1
  1. 次のように UIImage を初期化する必要があります。

    UIImage *image = [UIImage imageNamed:@"BluePin"];
    
  2. UIImageView を使用して UIImage を含める必要があります。UIImage を UIView に直接配置することはできません。

アップデート

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[[event allTouches]anyObject];
    CGPoint point= [touch locationInView:touch.view];

    UIImage *image = [UIImage imageNamed:@"BluePin"];
    CGRect rect=CGRectMake(point.x, point.y, image.size.width, image.size.height);
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:rect];
    [imageView setImage:image];
    [self.view addSubview:imageView];
}
于 2012-09-12T02:44:53.663 に答える
0

他の答えに追加するには、アニメーション化する方法を次に示します。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"Touches began!");
    UITouch *touch= [[event allTouches] anyObject];
    CGPoint point= [touch locationInView:touch.view];

    UIImage *image = [UIImage imageNamed:@"BluePin.png"];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame: CGRectMake(point.x-(imageView.bounds.size.width/2), point.y-(imageView.bounds.size.width/2), imageView.bounds.size.width, imageView.bounds.size.height)];
    [self addSubview: imageView];
    //self.currentPins += 1;

    [UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear  animations:^{
        [imageView setAlpha:0.0];
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        //self.currentPins -= 1;
    }];

   // for(;self.currentPins > 10; currentPins -= 1){
   //     [[[self subviews] objectAtIndex:0] removeFromSuperview];
   // }
}

コメントアウトされたコードは、 currentPins という@propertyがあると仮定して、画面上のピンの数を一度に 10 個に制限するために私が書いた少し余分なものです。数行をコピー、貼り付け、コメントアウトした後、何も台無しにしなかったと仮定して、テスト済みで動作します。

編集: コメントアウトされたコードは無視してください。私は実際に 2 つのバージョン (1 つはアニメーションなし、もう 1 つはアニメーションあり) を混同したので、壊れています。

于 2012-09-12T03:52:59.760 に答える