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
下面是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
相关文章推荐
- iOS开发中SDK是什么?
- iOS 苹果官方Demo合集
- iOS NSUserDefaults简介及使用
- iOS NSUserDefaults简介及使用
- ios pch文件内 常用的各类宏定义
- 【iOS】stringWithFormat 保留小数点位数 float double
- iOS——关于应用程序生命周期问题
- IOS与服务器交互POST 和 GET
- Xcode6以后中怎么添加空工程模板
- iOS开发之小文件下载、大文件下载、断点下载
- iOS学习笔记50-IOS后台运行 之 后台播放音乐
- iOS 游戏渠道SDK 抽象工程封装(上)
- iOS常用的四种数据持久化
- Core Data 批量更新(iOS 8 特性)
- 如何让iOS 保持界面流畅
- Core Data 异步查询(iOS 8 特性)
- iOS 保持界面流畅的技巧
- Core Data 简单查询、
- iOS 多线程的具体用法
- 【iOS学习笔记 15-12-1】用collectionView解决大量button的性能问题