Pra*_*ore 14 objective-c ios uipangesturerecognizer autolayout nslayoutconstraint
如何在iOS中使用平移手势平滑地修改约束变化?
我正在尝试开发一个屏幕,其中一个视图位于屏幕的底部.我已经为该视图添加了平移手势.在拖动该视图时,我想要更改该视图的顶部约束.平移手势仅允许在垂直和向下方向.我添加了一些拖动视图的限制.它工作但不顺利.如何使用平移手势平滑地修改约束变化?这是我的代码.
- (void)handleGesture:(UIPanGestureRecognizer *)sender
{
CGPoint velocity = [sender velocityInView:_locationContainer];
[sender setTranslation:CGPointMake(0, 0) inView:self.view];
if (fabs(velocity.y) > fabs(velocity.x)) {
NSLog(@"velocity y %f ",velocity.y * 0.13);
if(velocity.y < 0 && (self.locationDetailsTop.constant > minimumTop) )
{
NSLog(@"gesture moving Up");
self.locationDetailsTop.constant = self.locationDetailsTop.constant - fabs(velocity.y * 0.1);
}
else if (self.locationDetailsTop.constant < firstTop)
{
NSLog(@"gesture moving Bottom");
self.locationDetailsTop.constant = self.locationDetailsTop.constant + fabs(velocity.y * 0.1);
}
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.1 animations:^{
[self.mapView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.locationContainer.frame.origin.y)];
}];
}
}
Run Code Online (Sandbox Code Playgroud)
这是示例图像,我的屏幕与此类似,但在我的屏幕上,有一个地图视图而不是日历视图
要在用户触摸屏幕时移动视图,您可以使用translationInView:属性。您可以设置转换到当前约束的价值并获得新的价值(在一个处理器UIGestureRecognizerStateBegan),并在一个处理器更改约束常数UIGestureRecognizerStateChanged:
- (void)padRecognizerStateChanged:(UIPanGestureRecognizer*)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
[sender setTranslation:CGPointMake(0.0, [self getConstraintValue]) inView: _locationContainer];
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
[self setConstraintValue: [sender translationInView:_locationContainer].y];
[self.view setNeedsLayout];
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在用户将拇指举过屏幕向上或向下移动时移动视图,则可以使用速度。比如可以实现减速效果。
| 归档时间: |
|
| 查看次数: |
895 次 |
| 最近记录: |