gpt4 book ai didi

ios - 向 UIPangesture 添加边界

转载 作者:行者123 更新时间:2023-11-29 01:05:08 34 4
gpt4 key购买 nike

我在 subview 中有一个 UILabel 并且在 上有一个 panGesturepinchGesture UILabel。截至目前,我可以将 UILabel 移动到所有 View 中。我希望此 UILabel 确实位于 subView 的区域内。我将如何做到这一点?

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
CGPoint translation = [panGesture translationInView:panGesture.view.superview];

if (UIGestureRecognizerStateBegan == panGesture.state ||UIGestureRecognizerStateChanged == panGesture.state) {
panGesture.view.center = CGPointMake(panGesture.view.center.x + translation.x,
panGesture.view.center.y + translation.y);
[panGesture setTranslation:CGPointZero inView:self.view];
}
}

在这一行中,

CGPoint translation = [panGesture translationInView:panGesture.view.superview];

它正在将它设置为 superView,我试图将它设置为我的 subView,但我似乎无法弄明白。

最佳答案

这是我处理可拖动按钮并将其限制在主视图边界的代码希望这段代码能帮到你

 CGPoint translation = [recognizer translationInView:self.view];
CGRect recognizerFrame = recognizer.view.frame;
recognizerFrame.origin.x += translation.x;
recognizerFrame.origin.y += translation.y;

// Check if UIImageView is completely inside its superView
if (CGRectContainsRect(self.view.bounds, recognizerFrame)) {
recognizer.view.frame = recognizerFrame;
}
// Else check if UIImageView is vertically and/or horizontally outside of its
// superView. If yes, then set UImageView's frame accordingly.
// This is required so that when user pans rapidly then it provides smooth translation.
else {
// Check vertically
if (recognizerFrame.origin.y < self.view.bounds.origin.y) {
recognizerFrame.origin.y = 0;
}
else if (recognizerFrame.origin.y + recognizerFrame.size.height > self.view.bounds.size.height) {
recognizerFrame.origin.y = self.view.bounds.size.height - recognizerFrame.size.height;
}

// Check horizantally
if (recognizerFrame.origin.x < self.view.bounds.origin.x) {
recognizerFrame.origin.x = 0;
}
else if (recognizerFrame.origin.x + recognizerFrame.size.width > self.view.bounds.size.width) {
recognizerFrame.origin.x = self.view.bounds.size.width - recognizerFrame.size.width;
}
}

// Reset translation so that on next pan recognition
// we get correct translation value
[recognizer setTranslation:CGPointZero inView:self.view];

关于ios - 向 UIPangesture 添加边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494620/

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