|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
管理所有设备发生的事件比如屏幕旋转屏幕关闭或者一些其他的程序的控制逻辑也应该写在这里他的初始化函数是-(id)initWithNibName:(NSString*)nibNamebundle:(NSBundle*)nibBundle后面那个NibName是InterfaceBuilder里设计的界面现在IB已经集成到XCode里了1、UIGestureRecognizer先容
手势辨认在iOS上十分主要,手势操纵挪动设备的主要特性,极年夜的增添了挪动设备利用便利性。
iOS体系在3.2今后,为便利开辟这利用一些经常使用的手势,供应了UIGestureRecognizer类。手势辨认UIGestureRecognizer类是个笼统类,上面的子类是详细的手势,开辟这能够间接利用这些手势辨认。
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
下面的手势对应的操纵是:
- Tap(点一下)
- Pinch(二指往然蛲獠Χ绞本S玫降乃醴牛
- Rotation(扭转)
- Swipe(滑动,疾速挪动)
- Pan(拖移,慢速挪动)
- LongPress(长按)
UIGestureRecognizer的承继干系以下:
<br>
2、利用手势的步骤
利用手势很复杂,分为两步:
- 创立手势实例。当创立手势时,指定一个回调办法,当手势入手下手,改动、或停止时,回调办法被挪用。
- 增加到必要辨认的View中。每一个手势只对应一个View,当屏幕触摸在View的界限内时,假如手势和预定的一样,那就会回调办法。
ps:一个手势只能对应一个View,可是一个View能够有多个手势。
倡议在真机上运转这些手势,摹拟器操纵不太便利,大概招致你以为手势生效。
3、Pan拖下手势:
- UIImageView*snakeImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"snake.png"]];
- snakeImageView.frame=CGRectMake(50,50,100,160);
- UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handlePan:)];
- [snakeImageViewaddGestureRecognizer:panGestureRecognizer];
- [self.viewsetBackgroundColor:[UIColorwhiteColor]];
- [self.viewaddSubview:snakeImageView];
新建一个ImageView,然后增加手势
回调办法:
- -(void)handlePan:(UIPanGestureRecognizer*)recognizer
- {
- CGPointtranslation=[recognizertranslationInView:self.view];
- recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,
- recognizer.view.center.y+translation.y);
- [recognizersetTranslation:CGPointZeroinView:self.view];
- }
4、Pinch缩撒手势
- UIPinchGestureRecognizer*pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handlePinch:)];<pclass="p1">[<spanclass="s1">snakeImageView</span><spanclass="s2">addGestureRecognizer</span>:pinchGestureRecognizer];</p>
- -(void)handlePinch:(UIPinchGestureRecognizer*)recognizer
- {
- recognizer.view.transform=CGAffineTransformScale(recognizer.view.transform,recognizer.scale,recognizer.scale);
- recognizer.scale=1;
- }
5、Rotation扭转手势
- UIRotationGestureRecognizer*rotateRecognizer=[[UIRotationGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handleRotate:)];
- [snakeImageViewaddGestureRecognizer:rotateRecognizer];
- -(void)handleRotate:(UIRotationGestureRecognizer*)recognizer
- {
- recognizer.view.transform=CGAffineTransformRotate(recognizer.view.transform,recognizer.rotation);
- recognizer.rotation=0;
- }
<br>
<br>
增加了这几个手势后,运转看效果,程序中的imageView放了一个
/^/^
_|__|O|
//~_/
____|__________/
_______
`
||
//
//
//
//
//_----_
//_-~~-_||
((_-~_--_~-__/|
~-____-~_-~~-_~-_-~/
~-__-~~-__-~
~--______-~~-___-~
的图片,在摹拟器上拖动是没成绩的。缩放和扭转有点成绩,估量是由于在摹拟器上的摹拟的两个打仗点间隔在imageView的界限外了,以是操纵有效果。
倡议在真机上运转这个手势。
在摹拟器上缩放和选择的操纵技能:
能够把imageView的frame值设置年夜一点,按住alt键,按下触摸板(不按下不可),如许就能够扭转和缩放了。
6、增加第二个ImagView并增加手势
记着:一个手势只能增加到一个View,两个View固然要有两个手势的实例了
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- UIImageView*snakeImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"snake.png"]];
- UIImageView*dragonImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"dragon.png"]];
- snakeImageView.frame=CGRectMake(120,120,100,160);
- dragonImageView.frame=CGRectMake(50,50,100,160);
- [self.viewaddSubview:snakeImageView];
- [self.viewaddSubview:dragonImageView];
- for(UIView*viewinself.view.subviews){
- UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handlePan:)];
- UIPinchGestureRecognizer*pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handlePinch:)];
- UIRotationGestureRecognizer*rotateRecognizer=[[UIRotationGestureRecognizeralloc]
- initWithTarget:self
- action:@selector(handleRotate:)];
- [viewaddGestureRecognizer:panGestureRecognizer];
- [viewaddGestureRecognizer:pinchGestureRecognizer];
- [viewaddGestureRecognizer:rotateRecognizer];
- [viewsetUserInteractionEnabled:YES];
- }
- [self.viewsetBackgroundColor:[UIColorwhiteColor]];
- }
多增加了一条龙的view,两个view都能吸收下面的三种手势。运转效果以下:
<br>
7、拖动(pan手势)速率(以较快的速率拖放后view有滑行的效果)
怎样完成呢?
- -(void)handlePan:(UIPanGestureRecognizer*)recognizer
- {
- CGPointtranslation=[recognizertranslationInView:self.view];
- recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,
- recognizer.view.center.y+translation.y);
- [recognizersetTranslation:CGPointZeroinView:self.view];
- if(recognizer.state==UIGestureRecognizerStateEnded){
- CGPointvelocity=[recognizervelocityInView:self.view];
- CGFloatmagnitude=sqrtf((velocity.x*velocity.x)+(velocity.y*velocity.y));
- CGFloatslideMult=magnitude/200;
- NSLog(@"magnitude:%f,slideMult:%f",magnitude,slideMult);
- floatslideFactor=0.1*slideMult;//Increaseformoreofaslide
- CGPointfinalPoint=CGPointMake(recognizer.view.center.x+(velocity.x*slideFactor),
- recognizer.view.center.y+(velocity.y*slideFactor));
- finalPoint.x=MIN(MAX(finalPoint.x,0),self.view.bounds.size.width);
- finalPoint.y=MIN(MAX(finalPoint.y,0),self.view.bounds.size.height);
- [UIViewanimateWithDuration:slideFactor*2delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{
- recognizer.view.center=finalPoint;
- }completion:nil];
- }
代码完成剖析:
- 盘算速率向量的长度(估量年夜部分都忘了)这些常识了。
- 假如速率向量小于200,那就会失掉一个小于的小数,那末滑行会很短
- 基于速率和速率要素盘算一个尽头
- 确保尽头不会跑出父View的界限
- 利用UIView动画使view滑动到尽头
运转后,疾速拖动图象view摊开会看到view还会在本来的偏向滑行一段路。
8、同时触发两个view的手势
手势之间是互斥的,假如你想同时触发蛇和龙的view,那末必要完成协定
UIGestureRecognizerDelegate,
- @interfaceViewController:UIViewController<UIGestureRecognizerDelegate>
- @end
并在协定这个办法里前往YES。
- -(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizershouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
- {
- returnYES;
- }
把self作为代办署理设置给手势:
- panGestureRecognizer.delegate=self;
- pinchGestureRecognizer.delegate=self;
- rotateRecognizer.delegate=self;
如许能够同时拖动或扭转缩放两个view了。
9、tap点击手势
这里为了便利看到tap的效果,当点击一下屏幕时,播放一个声响。
为了播放声响,我们到场AVFoundation.framework这个框架。
- -(AVAudioPlayer*)loadWav:(NSString*)filename{
- NSURL*url=[[NSBundlemainBundle]URLForResource:filenamewithExtension:@"wav"];
- NSError*error;
- AVAudioPlayer*player=[[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:&error];
- if(!player){
- NSLog(@"Errorloading%@:%@",url,error.localizedDescription);
- }else{
- [playerprepareToPlay];
- }
- returnplayer;
- }
我会在最初例子代码给出完全代码,增加手势的步骤和后面一样的。
- #import<UIKit/UIKit.h>
- #import<AVFoundation/AVFoundation.h>
- @interfaceViewController:UIViewController<UIGestureRecognizerDelegate>
- @property(strong)AVAudioPlayer*chompPlayer;
- @property(strong)AVAudioPlayer*hehePlayer;
- @end
- -(void)handleTap:(UITapGestureRecognizer*)recognizer{
- [self.chompPlayerplay];
- }
运转,点一下某个图,就会播放一个咬工具的声响。
不外这个点击播放声响有点缺点,就是在渐渐拖动的时分也会播放。这使得两个手势重合了。怎样办理呢?利用手势的:requireGestureRecognizerToFail办法。
10、手势的依附性
在viewDidLoad的轮回里增加这段代码:
- [tapRecognizerrequireGestureRecognizerToFail:panGestureRecognizer];
意义就是,当假如pan手势失利,就是没产生拖动,才会动身tap手势。如许假如你有稍微的拖动,那就是pan手势产生了。tap的声响就不会收回来了。
11、自界说手势
自界说手势承继:UIGestureRecognizer,完成上面的办法:
- –touchesBegan:withEvent:
- –touchesMoved:withEvent:
- –touchesEnded:withEvent:
- -touchesCancelled:withEvent:
新建一个类,承继UIGestureRecognizer,代码以下:
.h文件
- #import<UIKit/UIKit.h>
- typedefenum{
- DirectionUnknown=0,
- DirectionLeft,
- DirectionRight
- }Direction;
- @interfaceHappyGestureRecognizer:UIGestureRecognizer
- @property(assign)inttickleCount;
- @property(assign)CGPointcurTickleStart;
- @property(assign)DirectionlastDirection;
- @end
.m文件
照着书上的例子,一个一个做下来,只要不是文盲,就都会的呀,没有体现我的一点点个人价值。所以我决定了,不能再这样下去 |
|