iOS--摇一摇

直接调用方法 就好

界面由2张图片构成,在摇动时,让图片上下移动即可

直接看代码:

#define Main_Screen_Height      [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width       [[UIScreen mainScreen] bounds].size.width

//头文件
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>

//图片
@property (nonatomic, strong) UIImageView *imgUp;
@property (nonatomic, strong) UIImageView *imgDown;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"摇一摇建群";
    
    _imgUp = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height/2)];
    _imgUp.image = [UIImage imageNamed:@"icon_up"];
    [self.view addSubview:_imgUp];
    _imgDown = [[UIImageView alloc] initWithFrame:CGRectMake(0, Main_Screen_Height/2, Main_Screen_Width, Main_Screen_Height/2)];
    _imgDown.image = [UIImage imageNamed:@"icon_down"];
    [self.view addSubview:_imgDown];
    
   
    
    [self  becomeFirstResponder];
    //已经不需要其它多余代码了
}

-(BOOL)canBecomeFirstResponder {

    return YES;

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"开始摇动");
    //添加“摇一摇”动画
    [self addAnimations];
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振动效果
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"结束摇动");
    if (motion ==UIEventSubtypeMotionShake ) {
        
        // 1.添加摇动动画
        [self addAnimations];
        // 见第四点, 推荐第四点的方法二
        
        // 2.设置播放音效
        SystemSoundID soundID;
        NSString *path = [[NSBundle mainBundle ] pathForResource:@"glass" ofType:@"wav"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
        // 添加摇动声音
        AudioServicesPlaySystemSound (soundID);
        
        // 3.设置震动
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    

}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"取消");
}

- (void)addAnimations {
    
    // 让imgup上下移动
    CABasicAnimation *translation2 = [CABasicAnimation animationWithKeyPath:@"position"];
    translation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //所改变属性的起始值
    translation2.fromValue = [NSValue valueWithCGPoint:CGPointMake(Main_Screen_Width/2, Main_Screen_Width/2-45)];
    //所改变属性的结束时的值
    translation2.toValue = [NSValue valueWithCGPoint:CGPointMake(Main_Screen_Width/2, 40)];
    //动画的时长
    translation2.duration = 0.5;
    //重复的次数。不停重复设置为 HUGE_VALF
    translation2.repeatCount = 1;
    //动画结束时是否执行逆动画
    translation2.autoreverses = YES;
    
    // 让imagdown上下移动
    CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
    translation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(Main_Screen_Width/2, CGRectGetMidY(self.imgDown.frame))];
    translation.toValue = [NSValue valueWithCGPoint:CGPointMake(Main_Screen_Width/2, CGRectGetMidY(self.imgDown.frame)+40)];
    translation.duration = 0.5;
    translation.repeatCount = 1;
    translation.autoreverses = YES;
    
    [self.imgDown.layer addAnimation:translation forKey:@"translation"];
    [self.imgUp.layer addAnimation:translation2 forKey:@"translation2"];
}

看图:

猜你喜欢

转载自my.oschina.net/huangyn/blog/1619814