0

是否有一种或两种惯用方式来处理下面描述的那种 UI 交互?也许不需要自定义类?

我正在 iPadd 应用程序中实现拖放,并希望处理可拖放对象未在可放置对象上释放并且平移手势离开UIView.

  • 当可拖动对象在视图上方时,视图会展开并获得一个边框,当可拖动对象离开该区域时,它将恢复到之前的大小并失去边框。在缩小动画开始之前会有明显的延迟。
  • 在缩小动画开始之前,可拖动对象可能会被带回该区域,这表明需要某种去抖动,即收集一段时间内发生的事件并将它们视为一个事件的东西。
  • 我知道在平移手势期间会触发大量事件,并且我不想分配不必要的资源(例如,计时器)。

我正在考虑使用单个自定义计时器,也许沿着这些思路,但也许有一些更简单的东西?

4

1 回答 1

0

每当手指在视图上移动时,以下代码将以 300 毫秒的 inflate-animation 使视图膨胀,并且只要触摸在外面,就会将视图放气回正常。不需要 panGestureRecognizer。

@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
于 2013-08-07T05:46:19.277 回答