setFrame无法使用子视图

时间:2012-04-10 19:34:07

标签: objective-c ios cocoa-touch uiview subview

我正在iOS5(iPad)上实现滑动抽屉。我通过继承UIView创建了抽屉。抽屉被添加到主视图中,工作正常。但是,当我尝试使用滑动手势和setFrame滑动抽屉开/关时,抽屉不会移动。

我相信我已经正确实现了手势识别器,并且帧也正确设置。但是,抽屉不动。对我做错了什么的想法?

以下是我的代码:

从我的控制器的viewDidLoad调用以下方法:

- (void)loadVerticalDrawer
{
    NSLog(@"LoadVerticalDrawer Executed");
    verticalDrawerHidden = YES;
    if (verticalDrawerHidden) {
    verticalDrawer = [[VerticalDrawer alloc] initWithFrame:CGRectMake(514, 250, 60, 248)];//adjust verticalDrawer height and width here;
    } else {
    verticalDrawer = [[VerticalDrawer alloc] initWithFrame:CGRectMake(464, 250, 60, 248)];//adjust verticalDrawer height and width here;    
    }

    verticalDrawer.appsManager = appsManager;
    verticalDrawer.delegate = self;
    [self.view addSubview:verticalDrawer];
}

还从viewDidLoad调用以下内容:

rightDrawerLeftSwipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightDrawerHandleSwipeLeft:)] autorelease];
    rightDrawerLeftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    rightDrawerLeftSwipe.numberOfTouchesRequired = 1;
    rightDrawerLeftSwipe.delegate = self;
    [verticalDrawer addGestureRecognizer:rightDrawerLeftSwipe];

    rightDrawerRightSwipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightDrawerHandleSwipeRight:)] autorelease];
    rightDrawerRightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    rightDrawerRightSwipe.numberOfTouchesRequired = 1;
    rightDrawerRightSwipe.delegate = self;
    [verticalDrawer addGestureRecognizer:rightDrawerRightSwipe];

最后,这是右滑动的处理程序:

-(void) rightDrawerHandleSwipeRight:(UISwipeGestureRecognizer*) recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        if (!verticalDrawerHidden){
        verticalDrawerHidden = YES;
        float x = verticalDrawer.frame.origin.x;
        float y = verticalDrawer.frame.origin.y;
        float width = verticalDrawer.frame.size.width;
        float height = verticalDrawer.frame.size.height;

        NSLog(@"Swipe left, Vertical drawer, x=%f, y=%f, width=%f, height=%f:", x,y,width,height);
        x+=50;
        [verticalDrawer setFrame:CGRectMake(x,y,width,height)];
        NSLog(@"Swipe left, Vertical drawer, x=%f, y=%f, width=%f, height=%f:", x,y,width,height);
        return;
        }
        else {
            return;
        }
    }
}

请注意,verticalDrawer的框架正确设置(并根据需要调用滑动处理程序),根据日志,它只是视图根本不移动!!

0 个答案:

没有答案