gpt4 book ai didi

iphone - UIView 的弹性拖动,就像从 UITableView 顶部拖动一样

转载 作者:行者123 更新时间:2023-12-01 17:58:09 26 4
gpt4 key购买 nike

我试图找出拖动 View 的逻辑,然后在拖动到某个点时以指数方式减慢速度。我有点让它工作了,虽然有车而且有点hacky。我想知道是否有更好的公式可以代替 newPosition = (-kDeleteViewWidth + (point.x * 0.2));
完整的手势代码如下。

// kDeleteViewWidth is defined as 80.0f
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint point = [gesture translationInView:self];

if ([gesture state] == UIGestureRecognizerStateBegan)
_initialPosition = _topView.frame.origin;

if ([gesture state] == UIGestureRecognizerStateChanged)
{

// Hacky Elastic method
float newPosition = _initialPosition.x + point.x;
if (point.x < -kDeleteViewWidth)
newPosition = (-kDeleteViewWidth + (point.x * 0.2));
[_topView setFrame:CGRectMake(newPosition, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}

最佳答案

为了完整起见,这是我使用的代码:

- (void)swipeCell:(id)sender
{
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint point = [gesture translationInView:self];

if ([gesture state] == UIGestureRecognizerStateBegan)
{
// CGPoint initialPosition
_initialPosition = _topView.frame.origin;
}

if ([gesture state] == UIGestureRecognizerStateChanged)
{

// Hacky Elastic method
float newPosition = _initialPosition.x + point.x;

// Starting point
if (_initialPosition.x == 0)
{
// Once we get to our pre-defined kDeleteViewWidth, start dividing by 5
if (_topView.frame.origin.x < -kDeleteViewWidth) {
float distance = (point.x - -kDeleteViewWidth);
float newDistance = distance / 5;
newPosition = (-kDeleteViewWidth + newDistance);
}

else if (_topView.frame.origin.x > 0)
{
float distance = point.x;
float newDistance = distance / 5;
newPosition = newDistance;
}
}

else if (_topView.frame.origin.x < -kDeleteViewWidth)
{
float distance = point.x;
float newDistance = distance / 5;
newPosition = (-kDeleteViewWidth + newDistance);
}

else if (_topView.frame.origin.x > 0)
{
float distance = point.x + _initialPosition.x;
float newDistance = distance / 5;
newPosition = newDistance;
}

[_topView setFrame:CGRectMake(newPosition, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}

if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) {

// If we have scrolled over a 1/3 of the way to kDeleteViewWidth, move to kDeleteViewWidth
if (point.x < -(kDeleteViewWidth / 3)) {
[UIView animateWithDuration:kAnimationDuration animations:^{
[_topView setFrame:CGRectMake(-kDeleteViewWidth, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}];
_isSwiped = YES;
}

// Otherwise animation back to 0
else {
[UIView animateWithDuration:kAnimationDuration animations:^{
[_topView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
} completion:^(BOOL finished) {
[self removeShadow];
}];
_isSwiped = NO;
}
}
}

关于iphone - UIView 的弹性拖动,就像从 UITableView 顶部拖动一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com