UITouch对象应用实例-触摸跟随特效,在ViewController中控制管理view接收的touch事件

////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
posted @ 2011-08-31 20:58  wujian1360  阅读(2884)  评论(0编辑  收藏  举报