【问题标题】:iPhone: modifying view when user shakesiPhone:用户摇晃时修改视图
【发布时间】:2011-02-26 17:01:57
【问题描述】:

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

- (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 方法是一种自定义单元格方法,用于在所选行上打勾。

【问题讨论】:

    标签: iphone objective-c cocoa-touch iphone-sdk-3.0 shake


    【解决方案1】:

    为了让你检查手机是否被晃动,你的班级必须使用 UIAccelerometerDelegate 协议。

    例如:

    @interface myTableViewClass : UITableView <UIAccelerometerDelegate>
    

    然后,您需要能够判断手机何时被摇动(我在 viewDidLoad 中使用了这个):

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

    一旦用户摇动手机,您就可以通过这种方法施展魔法:

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

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

    【讨论】:

      【解决方案2】:

      检查这些方法/API:

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

      这些是为运动识别提供的事件处理程序。在使用这些之前,请仔细阅读文档。

      【讨论】:

        猜你喜欢
        • 2011-07-11
        • 2011-10-24
        • 1970-01-01
        • 2010-09-14
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多