IOS开发-摇一摇效果
2015-07-14 14:28
411 查看
#import <AudioToolbox/AudioToolbox.h> 必须要加。
// ViewController.m
// yqs
//
// Created by 陈凯 on 15/6/24.
// Copyright (c) 2015年 leTian. All rights reserved.
//
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
@property UIView *yqsview;
@property UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_yqsview = [[UIView alloc]init];
_yqsview.frame = CGRectMake(100, 20, 200, 200);
[self.view addSubview:_yqsview];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
img.image = [UIImage imageNamed:@"1"];
[_yqsview addSubview:img];
//点击摇动 与 手机摇一摇 实现相同效果
_btn = [[UIButton alloc]initWithFrame:CGRectMake(160, 300, 40,20)];
[_btn setTitle:@"摇动" forState:UIControlStateNormal];
[_btn setTitleColor: [UIColor redColor] forState:UIControlStateNormal];
_btn.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_btn];
[_btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];//配置支持摇动
[self becomeFirstResponder];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
[self shakeView:_yqsview];
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
- (IBAction)click:(id)sender {
[self shakeView:_yqsview];
}
//抖动实现
- (void)shakeView:(UIView*)viewToShake
{
CGFloat t = 9.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.09 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:5.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
[UIView animateWithDuration:0.09 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ViewController.m
// yqs
//
// Created by 陈凯 on 15/6/24.
// Copyright (c) 2015年 leTian. All rights reserved.
//
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
@property UIView *yqsview;
@property UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_yqsview = [[UIView alloc]init];
_yqsview.frame = CGRectMake(100, 20, 200, 200);
[self.view addSubview:_yqsview];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
img.image = [UIImage imageNamed:@"1"];
[_yqsview addSubview:img];
//点击摇动 与 手机摇一摇 实现相同效果
_btn = [[UIButton alloc]initWithFrame:CGRectMake(160, 300, 40,20)];
[_btn setTitle:@"摇动" forState:UIControlStateNormal];
[_btn setTitleColor: [UIColor redColor] forState:UIControlStateNormal];
_btn.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_btn];
[_btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];//配置支持摇动
[self becomeFirstResponder];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
[self shakeView:_yqsview];
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
- (IBAction)click:(id)sender {
[self shakeView:_yqsview];
}
//抖动实现
- (void)shakeView:(UIView*)viewToShake
{
CGFloat t = 9.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.09 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:5.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished){
if(finished){
[UIView animateWithDuration:0.09 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS为什么Block的属性声明要用copy
- IOS 6 自动布局 入门-1(IOS中autolayout和之前版本autoresize的差异)
- (L1) AudioService AudioHandler setDeviceVolume, setAllVolumes, persistVolume
- IOS中判断设备型号的宏定义
- iOS正则表达式
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
- iOS解决两个静态库的冲突 duplicate symbol
- IOS 开发 手势使用
- IOS 添加libMobileVLCKit .a到项目中编译问题
- phonegap(cordova) 入门 5----小知识点总结------------ios 选择默认语言
- iOS 的事件处理
- 个人常用iOS第三方库以及XCode插件介绍
- [转] iOS --- 自定义CoreData迁移
- 【iOS开发系列】KVC&KVO
- iOS——MVVM设计模式
- ios修改NavigationController的背景颜色
- 中文 iOS/Mac 开发博客列表
- 小胖说事27-----IOS制作自已的静态framework
- ios开发 向右滑动手势实现返回.在NavigationController中如何设置
- phonegap(cordova) 入门 9----小知识点总结--iOS 状态栏的字体颜色都设置为白色