2

我正在 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];

最后,这是 Right Swipe 的处理程序:

-(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 的框架已正确设置(并且正在根据需要调用滑动处理程序),只是视图根本没有移动!

4

0 回答 0