Home  >  Q&A  >  body text

switch控件 - ios switch 是否震动 是否系统声音和是否接收新消息

第一次开发关于“调用iPhone的系统声音和震动,是否接收新消息”,首次用switch控件,在网上查了一些资料,但是还是一头雾水,我的代码和需求如下

导入框架:#import <AudioToolbox/AudioToolbox.h>
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.shake addTarget:self action:@selector(ChangeShake:) forControlEvents:UIControlEventValueChanged];
    [self.sound addTarget:self action:@selector(changeSound:) forControlEvents:UIControlEventValueChanged];
    // Do any additional setup after loading the view from its nib.
}
-(void)ChangeShake:(UISwitch *)sender{
    if (self.shake.isOn) {
        NSLog(@"开启状态————震动");
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//系统声音
//         AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);//警告声音
    }else{
        NSLog(@"关闭状态————震动");
    }
}
-(void)changeSound:(UISwitch *)sender{
    if (self.sound.isOn) {
        //播放声音
        NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"]; //音效文件路径
        SystemSoundID soundID;//组装并播放音效
        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
        AudioServicesPlaySystemSound(soundID);
        AudioServicesDisposeSystemSoundID(soundID);//声音停止
         NSLog(@"开启状态————声音");
    }else{
        NSLog(@"关闭状态————声音");
    }
}

以上这就是我的代码!
项目需求就像微信和QQ的设置那种效果!!!!!

求大神告知!!!!!!

怪我咯怪我咯2730 days ago741

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 14:25:01

    Post a piece of code that I use switch.

    - (void)viewDidLoad
    {
        [self.testswitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    
    - (void)switchAction:(id)sender
    {
        UISwitch *switchButton = (UISwitch*)sender;
        BOOL isButtonOn = [switchButton isOn];
        if (isButtonOn) {
            status = 0;
        } else {
            status = 1;
        }
    }
    

    Hope it helps you

    reply
    0
  • Cancelreply