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

Ios 拖动后如何正确设置框架?,ios,uibutton,uipangesturerecognizer,cgrect,cgpoint,Ios,Uibutton,Uipangesturerecognizer,Cgrect,Cgpoint,我正在使用故事板构建iOS应用程序 在我的应用程序中,我想创建一个自定义滑块。 为此,我以编程方式创建了两个视图和图像。 大视图被视为滑块。小的是指拇指。我用了拇指的触觉,这样它就可以移动了。它必须位于滑块中 我有两个按钮动作, 添加按钮用于仅在右侧添加拇指[小视图]的宽度。为此,我使用一个单位值,设为6。 减号按钮用于将拇指的宽度仅减少右侧相同的单位值 这些都很好用。拖动拇指[小视图]后出现问题。之后,如果单击“添加”按钮,x坐标位置将移动。我希望它保持不变。虽然NSLog x值,但在拖动并单

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

在我的应用程序中,我想创建一个自定义滑块。 为此,我以编程方式创建了两个视图和图像。 大视图被视为滑块。小的是指拇指。我用了拇指的触觉,这样它就可以移动了。它必须位于滑块中

我有两个按钮动作, 添加按钮用于仅在右侧添加拇指[小视图]的宽度。为此,我使用一个单位值,设为6。 减号按钮用于将拇指的宽度仅减少右侧相同的单位值

这些都很好用。拖动拇指[小视图]后出现问题。之后,如果单击“添加”按钮,x坐标位置将移动。我希望它保持不变。虽然NSLog x值,但在拖动并单击“添加”按钮后,我获得了相同的x值。我无法理解帧移动的原因。这是当前结果

我的预期结果是,当我拖动视图时,使用x坐标的PanGesture将保持不变。我想通过该位置增加帧的宽度

#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);
    }

}
#导入“ViewController.h”
@界面视图控制器()
{
UIView*bookingSlotView;
UIView*拇指;
UIImageView*旋钮;
UIPanGestureRecognizer*移动;
CGRect thumbRect;
int delta,thumbWidth,thumbXCord,flag;
}
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
δ=6;
拇指宽度=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添加子视图:thumb];
旋钮=[[UIImageView alloc]initWithFrame:CGRectMake(thumb.frame.origin.x,thumb.frame.origin.y+30,thumb.frame.size.width/2,thumb.frame.size.height+20)];
旋钮.image=[UIImage ImageName:@”games@2x.png"];
knob.center=CGPointMake(thumb.bounds.size.width/2,thumb.bounds.size.height);
[拇指添加子视图:旋钮];
move=[[UIPangestureRecognitizer alloc]initWithTarget:self action:@selector(handleDragDescriptionView:)];
thumb.userInteractionEnabled=YES;
[移动设置MaximumNumberOfTouchs:1];
[拇指添加手势识别器:移动];
}
-(void)handleDragDescriptionView:(UIPangestureRecognitor*)手势识别器{
flag=1;
CGPoint translation=[GestureRecognitor TranslationView:bookingSlotView];
if((gestureRecognitzer.state==UIgestureRecognitzerStateChanged)||
(gestureRecognizer.state==UIGestureRecognizerStateEnded))
{
thumb.center=CGPointMake(thumb.center.x+translation.x,thumb.center.y);
thumbXCord=(int)thumb.center.x;
NSLog(@“%d”,thumbXCord);
[手势识别器setTranslation:CGPointZero-inView:thumb];
}
}
-(iAction)增加:(UIButton*)发送器{
常数int newthumbXCord=thumbXCord;
if(thumb.frame.size.width20&&flag==0)
{
指宽-=三角洲;
[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
以说明宽度。

在平移手势中,拖动时出现问题的是更改中心。更改帧而不是中心我将CGPoint更改为帧。现在,拖动时它将移出屏幕。至少
thumbXCord=(int)(thumb.center.x-(thumb.frame.size.width*0.5));
但理想情况下,在所有案例中使用框架或中心,该任务都有效。还有一些案例,当我更改所有这些时,其工作正常。感谢您的宝贵考虑。