Search code examples
iosobjective-cuitouch

crash when using a category


I create a class category of UITouch this my code :

 - (id)initInView:(UIView *)view;
{
CGRect frame = view.frame;    
CGPoint centerPoint = CGPointMake(frame.size.width * 0.5f, frame.size.height * 0.5f);
return [self initAtPoint:centerPoint inView:view];
}

- (id)initAtPoint:(CGPoint)point inWindow:(UIWindow *)window;
{
 self = [super init];
if (self == nil) {
    return nil;
}

// Create a fake tap touch
_tapCount = 1;
_locationInWindow = point;
_previousLocationInWindow = _locationInWindow;

UIView *hitTestView = [window hitTest:_locationInWindow withEvent:nil];

_window = [window retain];
_view = [hitTestView retain];
if ([self respondsToSelector:@selector(setGestureView:)]) {
    [self setGestureView:hitTestView];
}
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [[NSProcessInfo processInfo] systemUptime];

return self;
}

- (id)initAtPoint:(CGPoint)point inView:(UIView *)view;
{
 return [self initAtPoint:[view.window convertPoint:point fromView:view] inWindow:view.window];
}

- (void)setPhase:(UITouchPhase)phase;
{
_phase = phase;
_timestamp = [[NSProcessInfo processInfo] systemUptime];
}

but when I call it I get this crash -[UITouch initAtPoint:inView:]: unrecognized selector sent to instance How can I fix that?


Solution

  • You say you created a category, but did not include the definition of your category.

    It should look something like this:

    //UITouch+customInitMethods.h
    
    
    @interface UITouch (customInitMethods)
    
    - (id)initInView:(UIView *)view;
    
    - (id)initAtPoint:(CGPoint)point inWindow:(UIWindow *)window;
    
    - (id)initAtPoint:(CGPoint)point inView:(UIView *)view;
    
    @end
    

    And then your implementation:

    #import "UITouch+customInitMethods.h"
    
    @implementation UITouch (customInitMethod)
    
    //Your method implementations go here.
    
    @end
    

    Make sure that the target checkbox on the .m file of your category file is set to include the category in your application target.

    Then you would need to #import UITouch+customInitMethods.h in any file that wanted to use your custom init methods.