UIView子类不响应触摸

问题描述:

我正在制作一个iPad项目,其中名为“Car”的类(这是来自视图控制器的单独文件)应该被拖动到主视图中。UIView子类不响应触摸

我按照我在Apple示例中看到的设置类,并且在运行应用程序时能够查看图像,但它像我的类不响应我的触摸事件,并且我无法解决问题。

这里是我的类代码: Car.h

#import <UIKit/UIKit.h> 


@interface Car : UIView { 

    UIImageView *firstPieceView; 
    CGPoint startTouchPosition; 

} 

-(void)animateFirstTouchAtPoint:(CGPoint)touchPoint forView:(UIImageView *)theView; 
-(void)animateView:(UIView *)theView toPosition:(CGPoint) thePosition; 
-(void)dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event; 
-(void)dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position; 
-(void)dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position; 

@property (nonatomic, retain) IBOutlet UIImageView *firstPieceView; 

@end 

,这是我的其他类代码:Car.m

#import "Car.h" 

    @implementation Car 

    @synthesize firstPieceView; 

    #define GROW_ANIMATION_DURATION_SECONDS 0.15 // Determines how fast a piece size grows when it is moved. 
    #define SHRINK_ANIMATION_DURATION_SECONDS 0.15 // Determines how fast a piece size shrinks when a piece stops moving. 


    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

     // Enumerate through all the touch objects. 
     NSUInteger touchCount = 0; 
     for (UITouch *touch in touches) { 
      // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
      [self dispatchFirstTouchAtPoint:[touch locationInView:self] forEvent:nil]; 
      touchCount++; 
     } 
    } 

    // Checks to see which view, or views, the point is in and then calls a method to perform the opening animation, 
    // which makes the piece slightly larger, as if it is being picked up by the user. 
    -(void)dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event 
    { 
     if (CGRectContainsPoint([firstPieceView frame], touchPoint)) { 
      [self animateFirstTouchAtPoint:touchPoint forView:firstPieceView]; 
     } 
    } 

    // Handles the continuation of a touch. 
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 

     NSUInteger touchCount = 0; 
     // Enumerates through all touch objects 
     for (UITouch *touch in touches) { 
      // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
      [self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self]]; 
      touchCount++; 
     } 
    } 

    // Checks to see which view, or views, the point is in and then sets the center of each moved view to the new postion. 
    // If views are directly on top of each other, they move together. 
    -(void)dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position 
    { 
     // Check to see which view, or views, the point is in and then move to that position. 
     if (CGRectContainsPoint([firstPieceView frame], position)) { 
      firstPieceView.center = position; 
     } 
    } 

    // Handles the end of a touch event. 
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     // Enumerates through all touch object 
     for (UITouch *touch in touches) { 
      // Sends to the dispatch method, which will make sure the appropriate subview is acted upon 
      [self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self]]; 
     } 
    } 

    // Checks to see which view, or views, the point is in and then calls a method to perform the closing animation, 
    // which is to return the piece to its original size, as if it is being put down by the user. 
    -(void)dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position 
    { 
     // Check to see which view, or views, the point is in and then animate to that position. 
     if (CGRectContainsPoint([firstPieceView frame], position)) { 
      [self animateView:firstPieceView toPosition: position]; 
     } 
    } 

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     // Enumerates through all touch object 
     for (UITouch *touch in touches) { 
      // Sends to the dispatch method, which will make sure the appropriate subview is acted upon 
      [self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self]]; 
     } 
    } 

    #pragma mark - 
    #pragma mark === Animating subviews === 
    #pragma mark 

    // Scales up a view slightly which makes the piece slightly larger, as if it is being picked up by the user. 
    -(void)animateFirstTouchAtPoint:(CGPoint)touchPoint forView:(UIImageView *)theView 
    { 
     // Pulse the view by scaling up, then move the view to under the finger. 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:GROW_ANIMATION_DURATION_SECONDS]; 
     theView.transform = CGAffineTransformMakeScale(1.2, 1.2); 
     [UIView commitAnimations]; 
    } 

    // Scales down the view and moves it to the new position. 
    -(void)animateView:(UIView *)theView toPosition:(CGPoint)thePosition 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:SHRINK_ANIMATION_DURATION_SECONDS]; 
     // Set the center to the final postion 
     theView.center = thePosition; 
     // Set the transform back to the identity, thus undoing the previous scaling effect. 
     theView.transform = CGAffineTransformIdentity; 
     [UIView commitAnimations]; 
    } 



    - (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 

      UIImage *img = [ UIImage imageNamed: @"CyanSquare.png" ]; 
      firstPieceView = [[UIImageView alloc] initWithImage: img]; 
      //[img release]; 
      [super addSubview:firstPieceView]; 
      [firstPieceView release]; 

     } 
     return self; 
    } 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    - (void)drawRect:(CGRect)rect 
    { 
     // Drawing code 
    } 
    */ 

    - (void)dealloc 
    { 
     [firstPieceView release]; 
     [super dealloc]; 
    } 

    @end 

这里是我的视图控制器代码:( ParkingviewController.h)

#import <UIKit/UIKit.h> 
#import "Car.h" 


@interface ParkingViewController : UIViewController { 

} 

@end 

最后但并非最不重要的ParkingViewController.m

#import "ParkingViewController.h" 

@implementation ParkingViewController 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 


    Car *car1 = [[Car alloc] init]; 
    [self.view addSubview:car1]; 
    [car1 release]; 
    [super viewDidLoad]; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 

@end 

请原谅我,如果我已经发布的所有代码,但我要明确在我的项目的每一个环节,使任何人都可以拥有整个情况是清楚的。

您需要为Car对象设置一个框架,以便处理触摸。您可以看到图像,默认情况下视图的clipsToBounds属性设置为NO