2010-09-24 85 views
1

嘿所有,任何人都可以请解释我如何可以子类UIButton并重写一些方法,以便当用户拖动一个按钮它立即出现?问题是,当我拖出按钮框架时,它仍然处于活动状态和停止状态。只要手指离开按钮框架,我希望它停止。有任何想法吗?可可触摸 - UIButtons - 继承UIButton

(可可触摸)

回答

4

如果有谁有这个问题,下面的代码允许极其精确的边缘检测的同时拖动。如果拖出按钮,它不会像平常一样延伸到按钮的边缘。

(我子类的UIButton并提出了如下:)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    touchBlocker = TRUE; 
    self.highlighted = TRUE; 
    NSLog(@"Touch Began"); 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    if (touchBlocker) { 
     if (!CGRectContainsPoint([self bounds], location)) { 
      touchBlocker =FALSE; 
      self.highlighted = FALSE; 
      NSLog(@"Touch Exit"); 
     } 
    } else if (CGRectContainsPoint([self bounds], location)) { 
     touchBlocker = TRUE; 
     self.highlighted = TRUE; 
     NSLog(@"Touch Enter"); 
    } 

} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    touchBlocker = FALSE; 
    self.highlighted = FALSE; 
    NSLog(@"Touch Ended"); 
}