2012-07-26 30 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