How to correctly subclass UIControl?

后端 未结 7 668
北海茫月
北海茫月 2020-12-04 09:26

I don\'t want UIButton or anything like that. I want to subclass UIControl directly and make my own, very special control.

But for some rea

7条回答
  •  感情败类
    2020-12-04 09:59

    I think that you forgot to add [super] calls to touchesBegan/touchesEnded/touchesMoved. Methods like

    (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event    
    (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
    

    aren't working if you overriding touchesBegan / touchesEnded like this :

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       NSLog(@"Touches Began");
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       NSLog(@"Touches Moved");
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       NSLog(@"Touches Ended");
    }
    

    But! All works fine if methods will be like :

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       [super touchesBegan:touches withEvent:event];
       NSLog(@"Touches Began");
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       [super touchesMoved:touches withEvent:event];
         NSLog(@"Touches Moved");
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       [super touchesEnded:touches withEvent:event];
       NSLog(@"Touches Ended");
    }
    

提交回复
热议问题