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

发布于 2024-09-03 19:42:14 字数 1241 浏览 0 评论 0原文

我正在开发一个 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 方法是一种自定义单元格方法,用于在所选行上放置刻度线。

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 = @"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 method is a custom cell method which is used to put a tickmark on the row selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-09-10 19:42:14

为了让您检查手机是否被摇晃,您的类必须使用 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
    }
}

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

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.

幸福还没到 2024-09-10 19:42:14

检查这些方法/API:

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

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

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

这些是为动作识别提供的事件处理程序。使用这些之前请先阅读文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文