拖动后如何正确设置框架?

How can i set Frame properly After dragging?

我正在使用故事板构建 iOS 应用程序。

在我的应用程序中,我想创建一个自定义滑块。 为此,我以编程方式创建了两个视图和图像。 大视图被视为滑块。小的表示 thumb.I 为拇指应用了 panGesture 以便它可以移动。它必须位于滑块中。

我有 2 个按钮操作, 添加按钮用于仅在右侧添加拇指[小视图]的宽度。为此,我使用单位 value.Let 为 6。 减号按钮用于仅将右侧的拇指宽度减小相同的单位值。

这些都工作正常。拖动拇指 [small view] 后出现问题。之后如果我单击添加按钮,x 坐标位置是 shifted.I 希望它保持 same.while NSLog x 值,拖动后我得到相同的 x 值并单击添加 Button.I 我无法理解为什么框架 shifting.This 是当前结果。

我的预期结果是,当我使用 PanGesture 拖动视图时 x 坐标将保持不变 same.And 我想将框架的宽度增加到那个位置

#import "ViewController.h"

@interface ViewController ()
{
   UIView *bookingSlotView;
   UIView *thumb;
   UIImageView *knob;
   UIPanGestureRecognizer *move;
   CGRect thumbRect;
   int delta,thumbWidth,thumbXCord,flag;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    delta=6;
    thumbWidth=40;
    thumbXCord=160;
    flag=0;

    thumbRect = CGRectMake(thumbXCord, 0.0f, thumbWidth, 10);

    bookingSlotView = [[UIView alloc]initWithFrame:CGRectMake(20, 150, 280, 10)];
    bookingSlotView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:bookingSlotView];

    thumb=[[UIView alloc]initWithFrame:thumbRect];
    thumb.backgroundColor=[UIColor greenColor];
    [bookingSlotView addSubview:thumb];

    knob=[[UIImageView alloc]initWithFrame:CGRectMake(thumb.frame.origin.x,thumb.frame.origin.y+30,thumb.frame.size.width/2,thumb.frame.size.height+20)];
    knob.image=[UIImage imageNamed:@"games@2x.png"];
    knob.center = CGPointMake( thumb.bounds.size.width / 2, thumb.bounds.size.height);
    [thumb addSubview:knob];

    move=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleDragDescriptionView:)];
    thumb.userInteractionEnabled=YES;
    [move setMaximumNumberOfTouches:1];
    [thumb addGestureRecognizer:move];


}
- (void)handleDragDescriptionView:(UIPanGestureRecognizer *)gestureRecognizer {

    flag=1;
     CGPoint translation = [gestureRecognizer translationInView:bookingSlotView];
    if ((gestureRecognizer.state == UIGestureRecognizerStateChanged) ||
        (gestureRecognizer.state == UIGestureRecognizerStateEnded))
        {
            thumb.center = CGPointMake(thumb.center.x + translation.x, thumb.center.y);
            thumbXCord=(int)thumb.center.x;
            NSLog(@"%d",thumbXCord);
            [gestureRecognizer setTranslation:CGPointZero inView:thumb];
       }
}
- (IBAction)increase:(UIButton *)sender {
    const int newthumbXCord = thumbXCord;
    if(thumb.frame.size.width<120 && flag==0)
        {
            thumbWidth += delta;
            CGRect newFrame1=CGRectMake(thumbXCord, 0.0f, thumbWidth,10 );
            [thumb setFrame:newFrame1];
            knob.center = CGPointMake( thumb.bounds.size.width / 2, thumb.bounds.size.height);
        }
    //After Dragging this condition Will execute
    if (flag==1 && thumb.frame.size.width<bookingSlotView.frame.size.width)
        {
            thumbWidth += delta;

            CGRect newFrame1=CGRectMake(newthumbXCord, 0.0f, thumbWidth,10 );
            [thumb setFrame:newFrame1];
            NSLog(@"%d---------ThumbXCoordinate",thumbXCord);
            knob.center = CGPointMake( thumb.bounds.size.width / 2, thumb.bounds.size.height);

        }
}
- (IBAction)decrease:(UIButton *)sender {
    if (flag==1 && thumbWidth>20)
    {
        thumbWidth -= delta;
        CGRect newFrame1=CGRectMake(thumbXCord, 0.0f, thumbWidth,10 );
        [thumb setFrame:newFrame1];
        knob.center = CGPointMake( thumb.bounds.size.width / 2, thumb.bounds.size.height);

    }

    if (thumbWidth>20 && flag==0)
    {
        thumbWidth -= delta;
        [thumb setFrame:CGRectMake(thumbXCord, 0.0f, thumbWidth, 10)];
        knob.center = CGPointMake( thumb.bounds.size.width / 2, thumb.bounds.size.height);
    }

}

您正在混合设置框架原点和框架中心。当您使用您设置的手势平移时

thumbXCord = (int)thumb.center.x;

所以你创建的偏移量是拇指宽度的一半。

要么与您的设置保持一致,要么更新 thumbXCord 以考虑宽度。