Xcode 删除触摸事件图像?

发布于 2024-11-30 13:13:13 字数 922 浏览 1 评论 0原文

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

- (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 中删除它们?为什么即使我使用随机,动画总是从左上角开始?

I'm using this code to display random images on the screen.

- (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]; 
}

after a couple of touches the screen gets kinda full. How do I remove them inside another IBAction? And why does the animation always start in the upper left corner even though I'm using random?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2024-12-07 13:13:13

假设已添加到视图中的唯一子视图是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); 移到您设置云框架的行下方,您应该已准备就绪。

Assuming that the only subviews that have been added to your view are clouds,

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

The cloud is always starting at the upper left because this line: cloud.frame = CGRectMake(0, 0, 300, 300);resets its origin to 0,0. Move cloud.center = CGPointMake(random() % 250, random() % 400); below the line where you set the cloud's frame and you should be all set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文