UIView拖动和缓和

时间:2014-05-21 10:20:45

标签: ios objective-c uiview

我有一个可以拖动的视图。现在,当我停止拖动时,我希望视图能够缓解,以便让我感觉更自然。

有没有人有想法?

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    CGPoint previous = [touch previousLocationInView:self];

    if (!CGAffineTransformIsIdentity(self.transform)) {
        location = CGPointApplyAffineTransform(location, self.transform);
        previous = CGPointApplyAffineTransform(previous, self.transform);
    }

    self.frame = CGRectOffset(self.frame,
                              (location.x - previous.x),
                              (location.y - previous.y));
}

干杯

1 个答案:

答案 0 :(得分:0)

我建议您在视图上使用UIPanGestureRecognizer来执行拖动操作。 您可以通过[recognizer translationInView:yourview]获取翻译,通过[recognizer velocityInView:yourview]获得速度。应用拖动效果的平移并使用力度创建所需的减速动画。