gpt4 book ai didi

iphone - 我目前有一个加速计 Action ,可以显示图像但不会清除/重置

转载 作者:行者123 更新时间:2023-11-29 04:43:15 25 4
gpt4 key购买 nike

为了练习,我创建了一个项目来试验 iPhone 的加速计功能。现在,当我在设备上运行该应用程序时,它会以空白屏幕开始,摇动手机并显示图像。

我必须强制关闭应用程序才能清除图像。 我希望有人能够提供一种解决方案,使图像重置,以便我可以根据需要多次重复该过程。(摇动手机,显示图像,清晰图像)我认为它需要计时器什么的,不确定。这是源代码。感谢您花时间阅读和提供帮助。

//  ViewController.m
// AccelTest
//


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)viewDidUnload
{
[super viewDidUnload];
}

-(void)viewWillAppear:(BOOL)animated{
[self startAccel];
[self view];

}

-(void)viewWillDisappear:(BOOL)animated{
[self stopAccel];
[self view];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}


-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

double const kThreshold = 2.0;
// double const kThreshold = 2.0;
if ( fabsf(acceleration.x) > kThreshold
|| fabsf(acceleration.y) > kThreshold
|| fabsf(acceleration.z) > kThreshold){


[self.view addSubview:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Icon.png"]]];

}
}


-(void)startAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = .25;
}

-(void)stopAccel{
UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = nil;
}


@end

最佳答案

以下是我的做法(没有 ARC),点击图像使其消失。

删除你的行:

[self.view addSubview:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Icon.png"]]];

并添加这些行:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon.png"]];
myImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rm:)];
[myImageView addGestureRecognizer:tapgr];
[tapgr release]; tapgr = nil;
[self.view addSubview:myImageView];
[myImageView release]; myImageView = nil;

然后向 View Controller 添加一个方法,以便在点击 UIImageView 时将其删除。

-(void)rm:(UITapGestureRecognizer *)tapgr {
[tapgr.view removeFromSuperview];
}

当该图像被点击一次时,将调用 rm: 方法,该方法将从 self.view 中删除该图像

关于iphone - 我目前有一个加速计 Action ,可以显示图像但不会清除/重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10038089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com