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

时间:2015-05-18 10:27:25

标签: ios uibutton uipangesturerecognizer cgrect cgpoint

我正在使用StoryBoards构建iOS应用程序。

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

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

这些都很好。拖动拇指[小视图]后出现问题。之后如果单击添加按钮,x坐标位置会移动。我希望它保持相同。而NSLog x值,我得到相同的x值拖动并单击添加按钮后我无法理解Frame为什么会移动。这是当前的结果。

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

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

}

1 个答案:

答案 0 :(得分:0)

您正在设置画面原点和画面中心之间进行混音。当你用手势平移时,你正在设置

thumbXCord = (int)thumb.center.x;

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

要么对您要设置或更新thumbXCord的内容保持一致,以便考虑宽度。