UITouch对象应用实例-触摸跟随特效,在ViewController中控制管理view接收的touch事件
2011-08-31 20:58
405 查看
////EventReporter10ViewController中的代码
#import <UIKit/UIKit.h> @interface EventReporter10ViewController : UIViewController { //在IB中创建一下四个控件,用来检测touch数据 IBOutlet UITextField *startField; IBOutlet UITextField *endField; IBOutlet UITextField *moveField; IBOutlet UILabel *bottomLabel; UIView *box; } -(void)manageTouches:(NSSet*)touches; -(void)dragView:(UIView*)aView withPoint:(CGPoint)point; -(void)touchMoveWithPoint:(CGPoint)touchPos; @end #import "EventReporter10ViewController.h" @implementation EventReporter10ViewController
////下面一个方法是利用在ReportView中获取到的touchs集合去控制程序中的各个元素做各种事情。下面的代码实现了一个类似于鼠标跟随的触摸跟随特效;
-(void)manageTouches:(NSSet*)touches{ for (UITouch *touch in touches) { if (touch.phase == UITouchPhaseBegan) { CGPoint touchPos = [touch locationInView:self.view]; startField.text =[NSString stringWithFormat:@"Begin:%3.0f,%3.0f",touchPos.x,touchPos.y]; }else if(touch.phase == UITouchPhaseMoved){ bottomLabel.text = @"Touch 正在移动中..."; CGPoint touchPos = [touch locationInView:self.view]; moveField.text = [NSString stringWithFormat:@"move:%3.0f,%3.0f",touchPos.x,touchPos.y]; [self touchMoveWithPoint:touchPos]; }else if(touch.phase == UITouchPhaseEnded){ if (touch.tapCount>1) { bottomLabel.text = [NSString stringWithFormat:@"Taps:%2i",touch.tapCount]; }else{ bottomLabel.text = @"等待交互..."; } CGPoint touchPos = [touch locationInView:self.view]; endField.text = [NSString stringWithFormat:@"End:%3.0f,%3.0f",touchPos.x,touchPos.y]; } } } -(void)touchMoveWithPoint:(CGPoint)touchPos{ UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 3, 3)]; [aview setBackgroundColor:[UIColor redColor]]; [self.view addSubview:aview]; [aview release]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; [aview setAlpha:0.0]; [UIView commitAnimations]; [self dragView:aview withPoint:touchPos]; if ([[self.view subviews] count]>50) { [[[self.view subviews] objectAtIndex:4] removeFromSuperview]; } } -(void)dragView:(UIView*)aView withPoint:(CGPoint)point{ aView.center = point; } - (void)dealloc { [startField release]; [endField release]; [moveField release]; [bottomLabel release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; } @end
////以下代码是创建UIView子类ReportView,将EventReporter10ViewController控制的View变成ReportView(它的子类),添加四个获取touchs集合的方法,
////并将集合通过manageTouches:touches方法传递给EventReporter10ViewController,这里使用的self.nextResponder获取其ViewController的指针
#import <UIKit/UIKit.h> @class EventReporter10ViewController; @interface ReportView : UIView { } @end
#import "ReportView.h" #import "EventReporter10ViewController.h" @implementation ReportView ////ViewController就是本ReportView的nextResponder ////以下方法是将获取到的NSSet *touches集合 传给 ViewController, ////这样就可以在ViewController里面接收ReportView的touch事件,并以此对ReportView控制或其他对象进行控制 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ EventReporter10ViewController *mainViewController = (EventReporter10ViewController *)self.nextResponder; [mainViewController manageTouches:touches]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ EventReporter10ViewController *mainViewController = (EventReporter10ViewController *)self.nextResponder; [mainViewController manageTouches:touches]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ EventReporter10ViewController *mainViewController = (EventReporter10ViewController *)self.nextResponder; [mainViewController manageTouches:touches]; }
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)dealloc { [super dealloc]; } @end
相关文章推荐
- 【Android应用实例之三】跟随手指的小球——自定义SurfaceView应用
- ViewGroup触摸事件的分发,拦截,消耗(dispatchTouchEvent , onInterceptTouchEvent , onTouchEvent)
- android 在ViewGroup中处理触摸事件 [Managing Touch Events in a ViewGroup]
- Android 判断触摸点是否在某个view内部,解决子childView与parentView的touch事件冲突
- MVC(Model -View-Controller)实例应用模式
- 触摸事件UITouch的应用
- 对象序列化(七):应用实例->asp.net ViewState
- Android recycleView的应用和点击事件实例详解
- 解决QWidget父对象加入QGraphicsView内导致当前对象接收不到鼠标移动事件问题
- UITouch 触摸事件处理(实例)
- [ IOS ] 视图控制对象ViewController的生命周期
- UITouch 触摸事件处理(实例)
- 触摸事件UITouch的应用
- android中View和ViewGroup对touch事件对象的处理方法
- IOS--UI--LessonTouchView 触摸事件 & 响应者链
- ASP.NET MVC(模型(Model),视图(View)和控制Controller)实践:实现身份验证权限管理
- iOS触摸事件UITouch应用详解
- 【Android View】Android中View对触摸事件的处理和传递dispatchTouchEvent、onInterceptTouchEvent
- android 管理ViewGroup中的触摸事件
- UITouch 触摸事件处理(实例)