您的位置:首页 > 移动开发 > IOS开发

iOS 摇一摇

2015-12-01 17:27 316 查看
直接上代码:

下面是view的代码(UI,控制,数据分离,mvc模式便于管理):

头文件

#import <UIKit/UIKit.h>

@interface ShakeAndShake :
UIView
@property(strong,nonatomic)
UIImageView* shake;//background

@property(strong,nonatomic)
UIButton* icon_btn;//icon

@property(strong,nonatomic)
UILabel* name;

@property(strong,nonatomic)
UILabel* distance;
@property(strong,nonatomic)
UIView* bottom;;//record for shake

@property(assign,nonatomic)
int Uid;

-(void)initContents:(id)model;

@end

实现部分

#import "ShakeAndShake.h"

#import "AppDelegate.h"

#import "TuYuUserViewController.h"

#define TUYU_BOTTOM_VIEW_H 80

#define TUYU_BOTTOM_VIEW_PADDING 15

#define TUYU_BOTTOM_VIEW_ICON_H 50

@implementation ShakeAndShake

{

UIImageView* person_image;

UILabel* person_font;

}

@synthesize icon_btn,name,distance,bottom,Uid;

-(void)initContents:(id)model

{

_shake = [[UIImageView
alloc] initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 -65,
110, 130,
130)];

_shake.image = [UIImage
imageNamed:@"bkg_shake"];

[self
addSubview:_shake];

person_image = [[UIImageView
alloc] initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 -22.0/2, [LYUtils
getScreenSize].height -TITLE_BAR_H -150,
22.0, 22.0)];

person_image.image = [UIImage
imageNamed:@"person"];

[self
addSubview:person_image];



person_font = [[UILabel
alloc] initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 -15.0/2, [LYUtils
getScreenSize].height -TITLE_BAR_H -120,
15.0, 19)];

person_font.text =
@"人";

person_font.font = [UIFont
systemFontOfSize:14];

person_font.textColor = [UIColor
colorWithRed:0.0/255
green:180.0/255
blue:205.0/255
alpha:1.0];

[self
addSubview:person_font];

bottom = [[UIView
alloc]
initWithFrame:CGRectMake(0, [LYUtils
getScreenSize].height -TITLE_BAR_H -
TUYU_BOTTOM_VIEW_H, [LYUtils
getScreenSize].width,
TUYU_BOTTOM_VIEW_H)];

bottom.backgroundColor = [UIColor
colorWithRed:220.0/255
green:220.0/255
blue:220.0/255
alpha:1.0];

[self
addSubview:bottom];



icon_btn = [[UIButton
alloc]
initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 - (TUYU_BOTTOM_VIEW_ICON_H
+8 +80)/2,
TUYU_BOTTOM_VIEW_PADDING, TUYU_BOTTOM_VIEW_ICON_H,
TUYU_BOTTOM_VIEW_ICON_H)];

icon_btn.layer.cornerRadius
= TUYU_BOTTOM_VIEW_ICON_H/2;

icon_btn.layer.masksToBounds =
YES;

[icon_btn
setContentMode:(UIViewContentModeScaleAspectFill)];

[icon_btn
setImage:[UIImage
imageNamed:@"4"]
forState:UIControlStateNormal];

[icon_btn
addTarget:self
action:@selector(iconBtnClicked)
forControlEvents:UIControlEventTouchUpInside];

[bottom
addSubview:icon_btn];



name = [[UILabel
alloc]
initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 - (TUYU_BOTTOM_VIEW_ICON_H
+8 +80)/2 +TUYU_BOTTOM_VIEW_ICON_H
+8,
TUYU_BOTTOM_VIEW_PADDING, 80,
20)];

name.text =@"爱茉莉";

name.textColor = [UIColor
blackColor];

name.font = [UIFont
systemFontOfSize:14];

[bottom
addSubview:name];



distance = [[UILabel
alloc]
initWithFrame:CGRectMake([LYUtils
getScreenSize].width/2 - (TUYU_BOTTOM_VIEW_ICON_H
+8 +80)/2 +TUYU_BOTTOM_VIEW_ICON_H
+8,
TUYU_BOTTOM_VIEW_PADDING +20 +5,
80,
20)];

distance.text =@"相距0.15km";

distance.textColor = [UIColor
colorWithRed:100.0/255
green:100.0/255
blue:100.0/255
alpha:1.0];

distance.font = [UIFont
systemFontOfSize:14];

[bottom
addSubview:distance];

}

-(void)iconBtnClicked

{//这里是跳转到icon对应的用户信息的viewController

TuYuUserViewController* user = [[TuYuUserViewController
alloc]
init];

[user initContentViews];

[user setUModelByID:Uid];

[[AppDelegate
getInstance].lyNaviViewController
pushViewController:user
animated:YES];

}

@end

下面是控制器部分:

头文件

#import "LYBaseViewController.h"

@interface TuYuFindShakeViewController :
LYBaseViewController

-(void)initContents:(id)model;

@end

实现部分

#import "TuYuFindShakeViewController.h"

#import "ShakeAndShake.h"

#import "AppDelegate.h"

#import "TuYuShakeSetupViewController.h"

#import <AudioToolbox/AudioToolbox.h>

#import "TuYuIndexHelper.h"

#import "LYImageManager.h"

#define TUYU_BOTTOM_VIEW_H 80

#define TUYU_BOTTOM_VIEW_PADDING 15

#define TUYU_BOTTOM_VIEW_ICON_H 50

#define SCREEN_Width [UIScreen mainScreen].bounds.size.width

#define SCREEN_height [UIScreen mainScreen].bounds.size.height

@implementation TuYuFindShakeViewController

{

UIView* selfContent;

UIView* line;

ShakeAndShake* shake;

SystemSoundID sound;//系统声音的id
取值范围为:1000-2000

NSTimer * timer;

SystemSoundID soundID;

}

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)initTitleBar//封装的titleBar

{

LYTitleViewInitParams* p = [[LYTitleViewInitParams
alloc]
init];

p.title =@"摇一摇";

p.titleTextColor = [UIColor
blackColor];

p.notHaveBackBtn =false;

p.backBtnImage =
@"BackBtn";


//隐藏摇一摇的设置按钮

// p.haveRightBtn1 = true;

// p.rightBtn1Image = @"setup";

[self
initWithTitleBarParams:p];



}

-(void)rightBtn1Clicked

{

TuYuShakeSetupViewController* setup = [[TuYuShakeSetupViewController
alloc]
init];

[setup initContents:nil];

[[AppDelegate
getInstance].lyNaviViewController
pushViewController:setup
animated:YES];

}

-(void)initContents:(id)model

{

DLog(@"--initContents");

//titleBar

[self
initTitleBar];



line = [[UIView
alloc] initWithFrame:CGRectMake(0,
TITLE_BAR_H -0.5, [LYUtils
getScreenSize].width,
0.5)];

line.backgroundColor = [UIColor
colorWithRed:220.0/255
green:220.0/255
blue:220.0/255
alpha:1.0];

//line.backgroundColor = [UIColor redColor];

[self.view
addSubview:line];



//shakeView部分

shake = [[ShakeAndShake
alloc] initWithFrame:CGRectMake(0,
TITLE_BAR_H,[LYUtils
getScreenSize].width, [LYUtils
getScreenSize].height -TITLE_BAR_H)];

shake.backgroundColor = [UIColor
whiteColor];

[shake
initContents:nil];

[self.view
addSubview:shake];




//第一次进去隐藏

[shake.bottom
setFrame:CGRectMake(0,
SCREEN_height, [LYUtils
getScreenSize].width,
TUYU_BOTTOM_VIEW_H)];

DLog(@"--摇一摇");

[[UIApplication
sharedApplication] setApplicationSupportsShakeToEdit:YES]; // 设置允许摇一摇功能

[self
becomeFirstResponder]; // 设置自己为第一响应者

}

#pragma mark - Shake方法

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//检测到摇动

DLog(@"--检测到摇动");

//摇动隐藏上次摇到的人的信息

[UIView
animateWithDuration:0.5
animations:^{

[shake.bottom
setFrame:CGRectMake(0,
SCREEN_height, [LYUtils
getScreenSize].width,
TUYU_BOTTOM_VIEW_H)];

}];



timer = [NSTimer
scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(playSystemShake)
userInfo:nil
repeats:YES];



//播放声音

NSString *path = [[NSBundle
mainBundle] pathForResource:@"shake_sound_male"
ofType:@"wav"];

AudioServicesCreateSystemSoundID( (__bridge
CFURLRef)[NSURL
fileURLWithPath:path], &soundID );

AudioServicesPlaySystemSound(
soundID );



//定时停止震动

[self
performSelector:@selector(stopShareAndSound)
withObject:nil
afterDelay:0.8];

}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//摇动取消

DLog(@"--震动结束");

}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//摇动结束

if (event.subtype ==
UIEventSubtypeMotionShake) {



//something happens

DLog(@"--摇动结束");



}



}

#pragma mark - system sound and share

- (void)playSystemShake

{

//动画

[UIView
animateWithDuration:0.5
animations:^{

shake.shake.transform=CGAffineTransformMakeRotation(3.14/3);

shake.shake.transform=CGAffineTransformMakeRotation(-3.14/3);

}];

//播放振动

sound =
kSystemSoundID_Vibrate;

AudioServicesPlaySystemSound(sound);



//获取用户信息

[TuYuIndexHelper
findShareTogether:0
cb:^(User *user) {



//图片加载

LYImageManagerParams* p = [[LYImageManagerParams
alloc]
init];

p.pathOrUrl = user.icon_url;

DLog(@"user icon:%@", p.pathOrUrl);

p.imageType =
PhotoTypeS;

p.cb = ^(LYImageManagerResult* iconR){

[shake.icon_btn
setImage:iconR.image
forState:UIControlStateNormal];

shake.Uid =user.uid;

//shake.icon_btn.imageView.image=iconR.image;

DLog(@"摇一摇---距离---%f-",user.distance);

};

[[LYImageManager
getInstance] getImage:p];

shake.name.text=user.show_name;

shake.distance.text=[NSString
stringWithFormat:@"相距%.2fkm", user.distance];



//显示摇到的人

[UIView
animateWithDuration:0.5
animations:^{



[shake.bottom
setFrame:CGRectMake(0, [LYUtils
getScreenSize].height -TITLE_BAR_H -
TUYU_BOTTOM_VIEW_H, [LYUtils
getScreenSize].width,
TUYU_BOTTOM_VIEW_H)];

shake.icon_btn.layer.cornerRadius
= TUYU_BOTTOM_VIEW_ICON_H / 2.0;

shake.icon_btn.layer.masksToBounds =
YES;

[shake.icon_btn
setContentMode:(UIViewContentModeScaleAspectFill)];

}];



}

];

}

-(void)stopShareAndSound

{

DLog(@"--摇动停止");

[timer
invalidate];

shake.shake.transform=CGAffineTransformMakeRotation(0);

//播放停止声音

NSString *path = [[NSBundle
mainBundle] pathForResource:@"shake_match"
ofType:@"wav"];

AudioServicesCreateSystemSoundID( (__bridge
CFURLRef)[NSURL
fileURLWithPath:path], &soundID );

AudioServicesPlaySystemSound(
soundID );

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: