【问题标题】:Xcode remove touch event images?Xcode删除触摸事件图像?
【发布时间】:2011-08-19 11:17:52
【问题描述】:

我正在使用此代码在屏幕上显示随机图像。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    UIImageView *cloud = [[UIImageView alloc] 
                               initWithImage:[UIImage imageNamed:@"spit.png"]];

    cloud.center = CGPointMake(random() % 250, random() % 400); //Random place
    [cloud setAlpha:1];
    [self.view addSubview:cloud];
    [cloud release];

    cloud.frame = CGRectMake(0, 0, 300, 300); 

    [UIView beginAnimations: @"cloddy" context: nil];
    [UIView setAnimationDuration: 0.5f];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseIn];

    // Size when done
    cloud.frame = CGRectMake(0, 0, 75, 75); 
    cloud.center = location;
    [UIView commitAnimations]; 
}

几次触摸后,屏幕有点满了。如何在另一个 IBAction 中删除它们?为什么我用的是随机的,动画总是从左上角开始?

【问题讨论】:

    标签: objective-c cocoa-touch uiview uiimageview uitouch


    【解决方案1】:

    假设已添加到您的视图中的唯一子视图是clouds

    - (IBAction)clearClouds:(id)sender{
        [[[self view] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    }
    

    云总是从左上角开始,因为这条线:cloud.frame = CGRectMake(0, 0, 300, 300);将其原点重置为 0,0。将cloud.center = CGPointMake(random() % 250, random() % 400); 移动到设置云框架的行下方,您应该已经设置好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多