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

标签 iphone ios xcode ios4 accelerometer

为了练习,我创建了一个项目来试验 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/

相关文章:

objective-c - iphone 图像褪色

iphone - 动画完成后想调用一些方法

iphone - 确定 UIView 的可用区域

ios - 在 SDK 谷歌地图中快速绘制多边形/形状

ios - Appcelerator SSL 证书无效

ios - UIColor vs IB 颜色(颜色配置文件问题)

iphone - uitextfield 中的光标位置

ios - 检查字符串是否包含 Swift 中的特殊字符

ios - 配置单元格时更改 View 位置。奇怪的行为

iphone - 如何将普通 View 颜色更改为棕褐色?