ios - NSTimer 用户信息。对象如何传递给选择器?

标签 ios nstimer userinfo

我有这个代码:

-(void)startRotation:(RDUtilitiesBarRotation)mode {
    rotationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(rotateSelectedItem:) userInfo:[NSNumber numberWithInt:mode] repeats:YES];
}
-(void)rotateSelectedItem:(NSNumber*)sender {
    float currAngle = [selectedItem currentRotation];
    if ([sender intValue] == RDUtilitiesBarRotationLeft) {
        [selectedItem rotateImage:currAngle - 1];
    }
    else {
        [selectedItem rotateImage:currAngle + 1];
    }
}
-(void)stopRotation {
    [rotationTimer invalidate];
    rotationTimer = nil;
}

目标是在用户按住按钮时开始旋转 View 。当用户释放它时,计时器将停止。

但是我给了这个:

-[__NSCFTimer intValue]: unrecognized selector sent to instance 0x4ae360

但是如果我在 userInfo 中传递一个 NSNumber 类,为什么我会收到计时器?

谢谢。

最佳答案

您的计时器操作方法应如下所示

-(void)rotateSelectedItem:(NSTimer*)sender

您可以通过以下方式获取 userInfo

NSNumber *userInfo = sender.userInfo;

关于ios - NSTimer 用户信息。对象如何传递给选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9522872/

相关文章:

ios - 我将在哪里获得用于实现 Azure B2c 身份验证的 kGraphURI 和 kScopes

ios - 每 5 秒保存一次

ios - 用户信息中带有自定义对象的 UNMutableNotificationContent

ios - 上传图片到Cloudinary IOS

iphone - 串联NSString不起作用

iphone - iPhone 后台运行定时器功能

ios - 观看 OS2 NSTimer 问题

oauth-2.0 - OpenID 为当前登录用户以外的其他用户连接 Userinfo 端点

ios - 无法访问 [AnyHashable :Any] - Swift 4 类型的值

ios - 在 iphone 5 和 iphone 6 中动态设置字体大小