iPhone:当用户抖动时修改视图

iPhone:当用户抖动时修改视图,iphone,objective-c,cocoa-touch,iphone-sdk-3.0,shake,Iphone,Objective C,Cocoa Touch,Iphone Sdk 3.0,Shake,我正在开发一个iPhone应用程序,当用户摇晃手机时,它会从表视图中删除行。我创建了一个基于导航的项目。现在,当用户摇动iPhone时,我希望导航栏的标题更改为“删除”,并在导航栏上显示一个删除按钮,显示在同一视图中。否则,当用户选择特定行时,它应该移动到下一个视图。我已经编写了以下代码,但它不起作用。请帮帮我 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

我正在开发一个iPhone应用程序,当用户摇晃手机时,它会从表视图中删除行。我创建了一个基于导航的项目。现在,当用户摇动iPhone时,我希望导航栏的标题更改为“删除”,并在导航栏上显示一个删除按钮,显示在同一视图中。否则,当用户选择特定行时,它应该移动到下一个视图。我已经编写了以下代码,但它不起作用。请帮帮我

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (isShaked == NO) 
    {       
    //logic to move to next view goes here.
    }   
    else 
    {

        self.title = @"Delete Rows";
        delete=[[UIBarButtonItem alloc] initWithTitle:@"Delete rows" style:  
UIBarButtonItemStyleBordered  target:self action:@selector(deleteItemsSelected)] ;

                 self.navigationItem.rightBarButtonItem=self.delete;
        MyTableCell *targetCustomCell = (MyTableCell *)[tableView  cellForRowAtIndexPath:indexPath];
        [targetCustomCell checkAction];
        [self.tempArray addObject: [myModal.listOfStates objectAtIndex:indexPath.row]];

        //[delete addTarget:self action:@selector(deleteItemsSelected:) forControlEvents:UIControlEventTouchUpInside];

        self.tempTableView = tableView;
    }
}

-(void)deleteItemsSelected
{
    [myModal.listOfStates removeObjectsInArray:tempArray];
    [tempTableView reloadData];
}

checkAction
方法是一种自定义单元格方法,用于在所选行上打勾。

为了让您检查手机是否受到震动,您的班级必须使用UIAccelerometerDelegate协议

例如:

@interface myTableViewClass : UITableView <UIAccelerometerDelegate>
一旦用户摇动手机,您就可以通过以下方法施展您的魔法:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{   
    if(fabsf(acceleration.x) > 2.2 || fabsf(acceleration.y) > 2.2 || fabsf(acceleration.z) > 2.2){
        //The user has shaken the iPhone
    }
}

显然,您可以更改间隔以更频繁地检查,并更改加速计上的参数:didAccelerate方法以满足您的需要。

检查这些方法/API:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
这些是为运动识别提供的事件处理程序。在使用这些工具之前,请仔细阅读文档

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event