开发者

iPhone: modifying view when user shakes

I am developing an iPhone application which deletes rows from a table view when the user shakes the phone. I have created a navigation-based project. Now when the user shakes the iPhone I want the title of the navigation bar to change to "DELETE" and a delete button to appear on the navigation bar, in the same view. Otherwise, when a user selects a particular row it should move to the next view. I have written the following code but it's not working. Please help me out.

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

        self.title = @"Del开发者_JAVA技巧ete 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 method is a custom cell method which is used to put a tickmark on the row selected.


In order for you to check if the phone's been shaken, your class will have to use the UIAccelerometerDelegate protocol.

For example:

@interface myTableViewClass : UITableView <UIAccelerometerDelegate>

Then, you need to be able to tell when the phone's been shaken (I use this in my viewDidLoad):

[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.1];

Once the user shakes the phone, you can do your magic in this method:

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

You can obviously change the interval to check more often and change the parameters on the accelerometer:didAccelerate method to suit your needs.


Check these methods/APIs:

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

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

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

These are event handlers provided for motion recognition. Go through the document before using these.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜