2011年7月10日 星期日

Stanford CS193P Note-05

Universal Applications: am I on an iPad?
    // iOS 3.2 以上
    BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);

-----------------------------------------------------------------------------------------------------------------

Gesture Recognizers (手勢 識別器)

◎ Gestures are handled by the class UIGestureRecognizer
    This class is "abstract." We only actually use "concrete(具體的) subclasses" of it.

◎ There are two sides to using a gesture recognizer
    1. Adding a gesture recognizer to a UIView to ask it to recognize that gesture.
    2. Providing the implementation of a method to "handle" that gesture
       when it happens.

◎ Adding a gesture recognizer to a UIView from a Controller
    - (void)viewDidLoad
    {
        UIView*panView=...;

        UIGestureRecognizer *pangr =

        [[UIPanGestureRecognizer alloc] initWithTarget:panView action:@selector(pan:)];
        /* 1. panning: moving something around with your finger.

           2. panView: the target to handle a pan gesture
           3. action method(pan:) will be sent to the target (the panView)
           4. the action message(pan:) takes one argument: UIGestureRecognizer
        */
        
         // add this gesture recognizer to the view's list of gestures that it recognizes.
         [panView addGestureRecognizer:pangr];

         // the view has taken ownership of this object, so we must release it
         [pangr release];
    }

    // Only UIView instances can recognize a gesture
    // (because UIViews handle all touch input).


◎ How do we implement the target of a gesture recognizer?
    Each concrete class provides some methods to help you do that.
    For example, UIPanGestureRecognizer provides 3 methods
    - (CGPoint)translationInView:(UIView *)aView; // 多遠
    - (CGPoint)velocityInView:(UIView *)aView;    // 速度
    - (void)setTranslation:(CGPoint)translation inView:(UIView *)aView; // 位移: 常設為 0

 
◎ what would action method(pan:) look like?
   - (void)pan:(UIPanGestureRecognizer *)sender
   {
       // If the gesture is continuous,
       // it'll move on to the Changed and eventually the Ended state

       if ((sender.state == UIGestureRecognizerStateChanged) ||
          (sender.state == UIGestureRecognizerStateEnded)) {
              // The new movement is added to the translation that was there before.
              CGPoint translation = [recognizer translationInView:self];

              // move something in myself (I'm a UIView) by translation.x and translation.y
              // for example, if I were a graph and my origin was set by
              // an @property called origin

              self.origin = CGPointMake(self.origin.x+translation.x,
                                          self.origin.y+translation.y);


              // Here we are resetting the "translation that was there before" to zero.
              [recognizer setTranslation:CGPointZero inView:self];
          }
   }

 
◎ Other Concrete Gestures
    1. UIPinchGestureRecognizer // 捏: 縮放
      
//note that this is NOT readonly(can reset each movement)
       @property CGFloat scale;
      
// note that this IS readonly; scale factor per second
       @property (readonly) CGFloat velocity;

    2. UIRotationGestureRecognizer // 旋轉
      
//note that this is NOT readonly; inradians
       @property CGFloat rotation;
      
// note that this IS readonly; radians per second
       @property (readonly) CGFloat velocity;

    3. UISwipeGestureRecognizer // 滑過
      
// what direction swipes you want
       @propertyUISwipeGestureRecognizerDirectiondirection;
      
// two finger swipes? or just one finger? more?
       @property NSUInteger numberOfTouchesRequired;

    4. UITapGestureRecognizer // 輕拍
      
//single tap or double tap or triple tap,etc.
       @propertyNSUIntegernumberOfTapsRequired;
      
//e.g., require two finger tap?
       @propertyNSUIntegernumberOfTouchesRequired;


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。