iOS oc 手机摇一摇震动带音效功能

导入

#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>

核心代码

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    
    if (motion == UIEventSubtypeMotionShake) {
        if (//判断是否登录) {
            //判断摇一摇是否生效
            if (![self isValildShake]) {
                return;
            }
            //默认震动效果
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        } else {
            //默认震动效果
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        }
    }
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    
    if (motion == UIEventSubtypeMotionShake) {
        if (//判断是否登录) {
            //判断摇一摇是否生效
            if (![self isValildShake]) {
                return;
            }
            //设置声音及震动
            [self setSoundForShake];
            //do something
            
        } else {
            //设置声音及震动
            [self setSoundForShake];
            //do something
        }
    }
}
/**
 判断摇一摇是否生效
 */
- (BOOL)isValildShake {
    //某个界面不生效条件
    if () {
        return NO;
    } else {
        return YES;
    }
}

/**
 设置摇一摇声音及震动
 */
- (void)setSoundForShake {
    // 设置播放音效
    SystemSoundID soundID;
    NSString *path = [[NSBundle mainBundle ] pathForResource:@"shake" ofType:@".mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    // 添加摇动声音
    AudioServicesPlaySystemSound (soundID);
    
    // 设置震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

具体内容自行填充,mp3文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS Objective-C 中可以使用系统提供的 Core Image 框架来实现人脸拍照功能。具体步骤如下: 1. 导入 Core Image 框架: ``` #import <CoreImage/CoreImage.h> ``` 2. 创建 AVCaptureSession 实例: ``` AVCaptureSession *session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetPhoto; ``` 3. 创建 AVCaptureDevice 实例: ``` AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; ``` 4. 创建 AVCaptureDeviceInput 实例: ``` AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:input]; ``` 5. 创建 AVCaptureStillImageOutput 实例: ``` AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG}; [output setOutputSettings:outputSettings]; [session addOutput:output]; ``` 6. 创建 AVCaptureVideoPreviewLayer 实例: ``` AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; previewLayer.frame = self.view.bounds; [self.view.layer addSublayer:previewLayer]; [session startRunning]; ``` 7. 拍照并获取人脸: ``` AVCaptureConnection *connection = [output connectionWithMediaType:AVMediaTypeVideo]; [output captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { UIImage *image = [UIImage imageWithData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]]; CIImage *ciImage = [[CIImage alloc] initWithImage:image]; NSDictionary *options = @{CIDetectorAccuracy: CIDetectorAccuracyHigh}; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:options]; NSArray *features = [detector featuresInImage:ciImage]; for (CIFaceFeature *feature in features) { if (feature.hasLeftEyePosition && feature.hasRightEyePosition && feature.hasMouthPosition) { // 人脸拍照成功,可以在这里处理拍照后的操作 } } }]; ``` 以上是一个简单的人脸拍照功能的实现方法,你可以根据实际需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值