导航

Touch Events and UIResponder(19)

Posted on 2014-04-16 11:34  0304  阅读(300)  评论(0编辑  收藏  举报

NSValue valueWithNonretainedObject 

http://stackoverflow.com/questions/8748736/weak-object-in-an-nsdictionary

http://stackoverflow.com/questions/3618479/how-to-create-weak-reference-in-objective-c

 

setNeedsLayout

setNeedsDisplay

 

[[self nextResponder] touchesBegan:touches withEvent:event]; 

 

 

For the More Curious: UIControl 

UIControl : UIView  ---->UIView : UIResponder

 

 

The class UIControl is the superclass for several classes in Cocoa Touch, including UIButton and UISlider. We’ve seen how to set the targets and actions for these controls. Now we can take a closer look at how UIControl overrides the same UIResponder methods you implemented in this chapter.

In UIControl, each possible control event is associated with a constant. Buttons, for example, typically send action messages on the UIControlEventTouchUpInside control event. A target registered for this control event will only receive its action message if the user touches the control and then lifts the finger off the screen inside the frame of the control. Essentially, it is a tap.

For a button, however, you can have actions on other event types. For example, you might trigger a method if the user removes the finger inside or outside the

frame. Assigning the target and action programmatically would look like this: 

 

[rButton addTarget:tempController action:@selector(resetTemperature:)

forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

 

Now consider how UIControl handles UIControlEventTouchUpInside.

// Not the exact code. There is a bit more going on!
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

// Reference to the touch that is ending UITouch *touch = [touches anyObject];

// Location of that point in this control's coordinate system CGPoint touchLocation = [touch locationInView:self];

// Is that point still in my viewing bounds?
if (CGRectContainsPoint([self bounds], touchLocation)) {

// Send out action messages to all targets registered for this event!

[self sendActionsForControlEvents:UIControlEventTouchUpInside]; } else {

// The touch ended outside the bounds, different control event

[self sendActionsForControlEvents:UIControlEventTouchUpOutside]; }

 

So how do these actions get sent to the right target? At the end of the UIResponder method implementations, the control sends the message sendActionsForControlEvents: to itself. This method looks at all of the target-action pairs the control has, and if any of them are registered for the control event passed as the argument, those targets are sent an action message.

However, a control never sends a message directly to its targets. Instead, it routes these messages through the UIApplication object. Why not have controls send the action messages directly to the targets? Controls can also have nil-targeted actions. If a UIControl’s target is nil, the UIApplication finds the first responder of its UIWindow and sends the action message to it.