2012-07-26 65 views
0

所以我想要做的是这个视图是让我可以上下滑动UIView(menuView)进出屏幕。所以在故事板/ IB中,我放置了一个UIView并将其连接到文件所有者和所有爵士乐。然后我进入程序的下面的代码。问题是当nib加载它时,并没有把UIView放在应该放在第一位的地方,然后滑动不起作用。我试过用这个按钮编程的方法,它工作正常。预先感谢您对此有任何帮助。可滑动的UIView菜单,响应UIGestureRecognizer

.h文件中

UIView *menuView; 
} 
@property (strong, nonatomic) IBOutlet UIView *menuView; 

.m文件

@synthesize menuView; 



- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// Do any additional setup after loading the view. 

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(swipe:)]; 
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown]; 
[self.view addGestureRecognizer:swipeGesture]; 


menuView = [[UIView alloc] initWithFrame:CGRectMake(0, -80, 320, 80)]; 
[menuView setFrame:CGRectMake(0, -80, 320, 80)]; 

[self.view addSubview:menuView]; 
} 


-(void)swipe:(UISwipeGestureRecognizer*)sender { 
[UIView animateWithDuration:0.5 animations:^{ 
    [menuView setFrame:CGRectMake(0, 0, 320, 80)]; 
}]; 
    } 



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

回答

0

你已经设置了menuView的情节提要/ IB出口。这意味着xib/nib/storyboard正在执行alloc-init和初始构图。所以,你应该删除此行:

menuView = [[UIView alloc] initWithFrame:CGRectMake(0, -80, 320, 80)]; 

您也可以从viewdidLoad删除下一行:

[menuView setFrame:CGRectMake(0, -80, 320, 80)]; 

在那里,它是多余的。它已经由“initWithFrame”完成 - 这已经由笔尖完成了。

另一方面,可能很难在屏幕外放置某些东西。所以你可能实际上恢复了setFrame:一行 - 但是在viewWillAppear而不是viewDidLoadviewWillAppear在之后被称为笔尖已经完成了它的事情,所以笔尖不会妨碍重新组装。

进行这些更改,如果“所有爵士乐”包括将menuView连接到参考插座,则应该很好。

+0

没有任何工作。 “菜单视图”仍然不会像编程时那样滑动。 – nfoggia 2012-07-27 05:58:17