使用View控制器的TabBar按钮在框架中添加滑动子视图

时间:2014-04-01 06:44:55

标签: ios iphone objective-c uiview frame

我在View Controller上有一个Tab-Bar按钮。我希望当用户按下该按钮时,从角落的子视图侧面显示指定操作的按钮列表,并再次按下Tab键按钮子视图应该向后滑动并消失。此子视图应显示在主视图的小框架上。我怎么能完成这个任务?

1 个答案:

答案 0 :(得分:0)

向该按钮添加自定义操作。 当用户按下按钮时,检查按钮视图的原点是否在主视图的范围内。如果不是,请将其滑入,否则将其滑出。

我可能会这样做:

//在viewDidLoad上调用此

- (void)createMenuButton {

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button addTarget:self action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];   
self.navigationItem.rightBarButtonItem = item;

}

//确保将menuView属性添加为self.view的子视图

- (void)toggleMenu {

if (CGRectContainsPoint(self.view.bounds, self.menuView.frame.origin)) {

    [UIView animateWithDuration:.35
                     animations:^{
                         CGRect rect = self.menuView.frame;
                         rect.origin.x -= self.menuView.frame.size.width;
                         self.menuView.frame = rect;
                     }];
} else {

    [UIView animateWithDuration:.35
                     animations:^{
                         CGRect rect = self.menuView.frame;
                         rect.origin.x += self.menuView.frame.size.width;
                         self.menuView.frame = rect;
                     }];
}

}

希望这会有所帮助:)

相关问题