2013-08-07 18 views
0

是否有一種或兩種慣用的方式來處理下面描述的那種UI交互?也許一個習慣課是不必要的?當平移手勢離開UIView時有延遲動畫,並有反彈?

我正在執行拖放在iPadd應用程序中,並且想要處理可拖動和拖動手勢離開UIView時未釋放拖動的情況。

  • 該視圖展開並在可拖動結束時獲取邊框,並且當可拖動的區域離開該區域時,它將返回到其以前的大小並丟失邊框。在縮小比例動畫開始之前會有明顯的延遲。
  • 在縮小動畫開始之前,可拖動的區域會被帶回區域,這表明需要某種類型的debouncing,即收集在一段時間內發生的事件並將它們視爲一個事件。
  • 我知道大量事件在平移手勢期間被觸發,我不想分配不必要的資源(例如計時器)。

我正在考慮使用一個自定義計時器,或許沿着these lines,但也許有更簡單的東西呢?

回答

0

只要手指移動到視圖上,下面的代碼就會以300毫秒的充氣動畫膨脹視圖,並且只要觸摸位於外面,視圖就會縮回視圖。沒有panGestureRecognizerRequired。

@interface CustomView : UIView 
{ 
    BOOL hasExpanded; 
    CGRect initialFrame; 
    CGRect inflatedFrame; 
} 
@end 

@implementation CustomView 
-(id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if(self) 
    { 
     hasExpanded = NO; 

     initialFrame = frame; 

     CGFloat inflateIncrement = 50.0f; 

     inflatedFrame = CGRectMake(self.frame.origin.x-(inflateIncrement*0.5f), 
             self.frame.origin.y-(inflateIncrement*0.5f), 
             self.frame.size.width+inflateIncrement, 
             self.frame.size.height+inflateIncrement); 

    } 
    return self; 
} 


-(void)forceDeflate 
{ 
    if (hasExpanded) 
    { 
     //start deflating view animation 
     [UIView animateWithDuration:0.3 animations:^{ 
      self.frame = initialFrame; 

     }]; 
     hasExpanded = NO; 
    } 
} 


-(void)inflateByCheckingPoint:(CGPoint)touchPoint 
{ 
    if(!hasExpanded) 
    { 
     if(CGRectContainsPoint(self.frame,touchPoint)) 
     { 
      //start inflating view animation 
      [UIView animateWithDuration:0.3 animations:^{ 
       self.frame = inflatedFrame; 

      }]; 

      hasExpanded = YES; 
     } 

    } 
    else 
    { 
     if(!CGRectContainsPoint(self.frame,touchPoint)) 
     { 
      //start deflating view animation 
      [UIView animateWithDuration:0.3 animations:^{ 
       self.frame = initialFrame; 

      }]; 

      hasExpanded = NO; 
     } 
    } 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *singleTouch = [touches anyObject]; 
    CGPoint touchPoint = [singleTouch locationInView:self.superview]; 
    [self inflateByCheckingPoint:touchPoint]; 
} 

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch *singleTouch = [touches anyObject]; 
    CGPoint touchPoint = [singleTouch locationInView:self.superview]; 
    [self inflateByCheckingPoint:touchPoint]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self forceDeflate]; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self forceDeflate]; 
} 

@end