touch手势与触摸

1.不能让子图层移动到父图层之外 
Java代码   收藏代码
  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  2.       
  3.     CGPoint pt = [[touches anyObject] locationInView:self];  
  4.     startLocation = pt;  
  5.     //将选中的图片显示在最前  
  6.     [[self superview] bringSubviewToFront:self];  
  7. }  
  8.   
  9. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
  10.       
  11.     CGPoint pt = [[touches anyObject] locationInView:self];  
  12.     float dx = pt.x - startLocation.x;  
  13.     float dy = pt.y - startLocation.y;  
  14.     CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);  
  15.       
  16.     //计算X的边界  
  17.     float halfx = CGRectGetMidX(self.bounds);  
  18.     newcenter.x = MAX(halfx , newcenter.x);  
  19.     newcenter.x = MIN(self.superview.bounds.size.width - halfx , newcenter.x);  
  20.       
  21.     //计算Y的边界  
  22.     float halfy = CGRectGetMidX(self.bounds);  
  23.     newcenter.y = MAX(halfy , newcenter.y);  
  24.     newcenter.y = MIN(self.superview.bounds.size.height - halfy , newcenter.y);  
  25.       
  26.     self.center = newcenter;  
  27. }  


2.如果图片为圆形,因为iphone事件是以一个矩形为事件体,只点击了圆形部分才接收事件 

Java代码   收藏代码
  1. - (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event   
  2.   
  3. /* 
  4.  判断点是否在园内: 
  5.  (1)看点与圆心的距离,园方程X^2+Y^2=R^2中圆心为(0,0)半径为R.在计算所求点与圆心的距离。计算方法:若所求点坐标为(M,N)则比较M^2+N^2与R^2的大小,前大则点在园外,前小则点在园内,相等则点在园上. 
  6.  (2)基本式(X-a)^2+(Y-b)^2=r^2中园心坐标为(a,b),在判断所求点与圆心坐标的距离即可。同样若所求点坐标为(M,N),则看(M-a)^2+(N-b)^2与r^2的大小,判断方法同上. 
  7.   
  8.  最后,要判断点是否在园内,只有比较点到圆心坐标的距离就行了大于半径在园外,小于半径在园内,等于在圆上. 
  9.  */  
  10. {  
  11.     NSLog(@"x=%f,y=%f",point.x,point.y);  
  12.     CGPoint pt;  
  13.     //点X,Y,都为32  
  14.     pt.x = (point.x - 32) / 32;  
  15.     pt.y = (point.y - 32) / 32;  
  16.       
  17.     float xsquared = pt.x * pt.x;  
  18.     float ysquared = pt.y * pt.y;  
  19.       
  20.     if (xsquared + ysquared <1.0f) return YES;  
  21.     return NO;  
  22.       
  23. }  


3.添加一个撤消管理器,在一个主要的ViewControl里面添加,其它子图层都会共享这个管理器. 
Java代码   收藏代码
  1. //ViewControl,添加一个撤销管理器  
  2.     self.undoManager = [[NSUndoManager alloc] init];  
  3.     [self.undoManager setLevelsOfUndo:999];  

ViewControl,里面添加两个button实现前进与后退 
Java代码   收藏代码
  1. - (void) undo {  
  2.       
  3.     [self.undoManager undo];  
  4. }  
  5.   
  6. - (void) redo {  
  7.       
  8.     [self.undoManager redo];  
  9. }  

子图层 
Java代码   收藏代码
  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  2.       
  3.     [self setPosition:self.center];  
  4.          ..........  

Java代码   收藏代码
  1. - (void) setPosition: (CGPoint) pos  
  2. {  
  3.     // Prepare undo-redo first  
  4.     //注册方法  
  5.     [[self.undoManager prepareWithInvocationTarget:self] setPosition:self.center];  
  6.       
  7.     // Make the change  
  8.     [UIView beginAnimations:@"" context:nil];  
  9.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  10.     [UIView setAnimationDuration:0.1f];  
  11.     self.center = pos; // animate  
  12.     [UIView commitAnimations];  
  13. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fingers is your one stop choice for all gesture handling in Unity (and other C# platforms as Fingers has no Unity specific code). Fingers has been tested and runs great on MAC, Windows, iOS and Android. Full C# source code and shader code is provided! No other asset matches Fingers Gestures on price and feature set. Fingers - Touch Gesture for Unity has been featured in multiple Unity promotions and tutorial videos, showing the high quality and full feature set and value proposition. With Fingers, you get all the common touch gestures you are used to built in. This includes: tap, double tap, long tap, pan, rotate, scale and swipe gestures. In addition, creating your own custom gestures is simple and easy. I've recently began adding on-screen controls. Right now a joystick and dpad are included with more controls planned for the future. Adding gestures in the inspector has never been easier. Simply add from the component menu and configure your gesture. Set the callback to your script and you are done! Pan/rotate/scale and drag and drop is simple. Built in scripts for these scenarios, with demo scenes are provided. I've also added image recognition gestures. With a few minutes of tweaking, you can recognize shapes like circles, lightning bolts, check marks and the letter X. Previewing gestures in the player is easy, even without a touch screen. Just use the shift and control key plus mouse wheel to simulate pinch and rotate gestures, along with each button to simulate a different finger. Fingers has a similar architecture to UIGestureRecognizer from iOS, but is built using all C# code and will easily run on any platform that has a mouse or touch screen. Gestures are restricted to most UI elements by default, but this is easily configured if you want the gestures to pass through additional UI elements. This asset works great with the default Unity UI and Event System. Gestures can be executed simultaneously or exclusively (the default). In addition
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值