码迷,mamicode.com
首页 > 其他好文 > 详细

使用加速计实现摇一摇动画效果

时间:2016-05-20 11:46:35      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

加入两张图片和mav声音的文件

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIImageView *imageView;

@end

.m文件

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic,strong) UIImage *fixed;
@property (nonatomic,strong) UIImage *broken;
@property (nonatomic,assign) BOOL brokenScreenShowing;
@property (nonatomic,strong) AVAudioPlayer *crashPlayer;

@end

@implementation ViewController

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"SecondBeep" withExtension:@"wav"];
    
    NSError *error = nil;
    self.crashPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    if (!self.crashPlayer) {
        
        NSLog(@"Audio Error! %@",error.localizedDescription);
    }
    
    self.fixed = [UIImage imageNamed:@"bg_登录、注册@2x.png"];
    self.broken = [UIImage imageNamed:@"bg_我的代金券_过期_右@2x.png"];
    self.imageView.image = self.fixed;
    
 
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (!self.brokenScreenShowing && motion == UIEventSubtypeMotionShake) {
        self.imageView.image = self.broken;
        [self.crashPlayer play];
        self.brokenScreenShowing = YES;
    }


}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.imageView.image = self.fixed;
    self.brokenScreenShowing = NO;
}

@end

使用加速计实现摇一摇动画效果

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5511142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!