iOS-OC-仿微信搖一搖

ZFJ_張福傑發表於2016-01-25

//
//  ViewController.m
//  搖一搖
//
//  Created by ZFJ_APPLE on 16/1/25.
//  Copyright © 2016年張福傑. All rights reserved.
//

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

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()


@property (nonatomic,strong) UIImageView *upImage;      //背景圖片

@property (nonatomic,strong) UIImageView *downImage;    //上一半手的圖片

@property (nonatomic,strong) UIView *upView;            //下一半手

@property (nonatomic,strong) UIView *downView;          //下一半手的圖片


@end

@implementation ViewController

- (void)viewDidLoad
{
    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self uiConfig];
}

- (void)uiConfig
{
    UIImageView *image = [[UIImageViewalloc]initWithFrame:CGRectMake(0,kHeight / 4,kWidth, kHeight /2)];
    image.image = [UIImageimageNamed:@"ShakeHideImg_women@2x.png"];
    [self.viewaddSubview:image];
    
    _upView = [[UIView alloc]initWithFrame:CGRectMake(0,0, kWidth, kHeight / 2)];
    _upView.backgroundColor = [UIColor blackColor];
    
    _upImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, kHeight /4, kWidth - 100, kHeight /4)];
    _upImage.image = [UIImage imageNamed:@"Shake_Logo_Up@2x.png"];
    
    [_upView addSubview:_upImage];
    [self.view addSubview:_upView];
    
    
    
    _downView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight /2, kWidth, kHeight / 2)];
    _downView.backgroundColor = [UIColor blackColor];
    
    _downImage = [[UIImageView alloc]initWithFrame:CGRectMake(50,0, kWidth - 100, kHeight /4)];
    _downImage.image = [UIImage imageNamed:@"Down@2x.png"];
    
    [_downView addSubview:_downImage];
    [self.view addSubview:_downView];
}

#pragma mark - 開始搖晃就會呼叫
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //開始搖晃設定動畫
    [UIView animateWithDuration:1 animations:^{
        
        _upView.frame = CGRectMake(0, -(kHeight /4), kWidth, kHeight / 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            _upView.frame = CGRectMake(0, 0, kWidth, kHeight / 2);
            
        }];
    }];
    
    [UIView animateWithDuration:1 animations:^{
        
        _downView.frame = CGRectMake(0, kHeight /2 + kHeight / 4, kWidth, kHeight /2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
           
            _downView.frame = CGRectMake(0, kHeight /2, kWidth, kHeight / 2);

        }];
        
    }];
    //播放搖晃聲音
    [LZAudioTool playMusic:@"1.mp3"];
}

#pragma mark - 搖晃結束就會呼叫
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    //搖晃結束
}

#pragma mark - 搖晃被打斷就會呼叫
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //搖晃被打斷
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



Demo下載地址:http://pan.baidu.com/s/1bohohIJ

相關文章