代碼:
-(void)createSelectionMenu {
selectionMenu = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH, 320)];
UIView *background = [[UIView alloc] init];
background.frame = CGRectMake(0, 0, SCREEN_WIDTH, 320);
background.contentMode = UIViewContentModeScaleToFill;
background.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
[selectionMenu addSubview:background];
UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
cancelButton.frame = CGRectMake(10, 260, SCREEN_WIDTH - 20, 50);
cancelButton.backgroundColor = [UIColor colorWithRed:132/255.0 green:139/255.0 blue:149/255.0 alpha:1.0];
cancelButton.layer.cornerRadius = 5;
cancelButton.layer.borderWidth = 1;
cancelButton.layer.borderColor = [[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1.0] CGColor];
[cancelButton addTarget:self action:@selector(hideSelectionMenu) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
cancelButton.titleLabel.font = [UIFont fontWithName: @"STHeitiSC-Medium" size: 20];
[selectionMenu addSubview: cancelButton];
UIButton *girlButton = [UIButton buttonWithType: UIButtonTypeCustom];
girlButton.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 50);
girlButton.backgroundColor = [UIColor whiteColor];
girlButton.layer.cornerRadius = 5;
girlButton.layer.borderWidth = 1;
girlButton.layer.borderColor = [[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1.0] CGColor];
[girlButton addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
[girlButton setTitle:@"女生" forState:UIControlStateNormal];
[girlButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[girlButton setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
girlButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
girlButton.titleLabel.font = [UIFont fontWithName: @"STHeitiSC-Medium" size: 20];
[selectionMenu addSubview: girlButton];
UIButton *boyButton = [UIButton buttonWithType: UIButtonTypeCustom];
boyButton.frame = CGRectMake(10, 70, SCREEN_WIDTH - 20, 50);
boyButton.backgroundColor = [UIColor whiteColor];
boyButton.layer.cornerRadius = 5;
boyButton.layer.borderWidth = 1;
boyButton.layer.borderColor = [[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1.0] CGColor];
[boyButton addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
[boyButton setTitle:@"男生" forState:UIControlStateNormal];
[boyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[boyButton setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
boyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
boyButton.titleLabel.font = [UIFont fontWithName: @"STHeitiSC-Medium" size: 20];
[selectionMenu addSubview: boyButton];
UIButton *coupleButton = [UIButton buttonWithType: UIButtonTypeCustom];
coupleButton.frame = CGRectMake(10, 130, SCREEN_WIDTH - 20, 50);
coupleButton.backgroundColor = [UIColor whiteColor];
coupleButton.layer.cornerRadius = 5;
coupleButton.layer.borderWidth = 1;
coupleButton.layer.borderColor = [[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1.0] CGColor];
[coupleButton addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
[coupleButton setTitle:@"情侶" forState:UIControlStateNormal];
[coupleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[coupleButton setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
coupleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
coupleButton.titleLabel.font = [UIFont fontWithName: @"STHeitiSC-Medium" size: 20];
[selectionMenu addSubview: coupleButton];
UIButton *allButton = [UIButton buttonWithType: UIButtonTypeCustom];
allButton.frame = CGRectMake(10, 190, SCREEN_WIDTH - 20, 50);
allButton.backgroundColor = [UIColor whiteColor];
allButton.layer.cornerRadius = 5;
allButton.layer.borderWidth = 1;
allButton.layer.borderColor = [[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1.0] CGColor];
[allButton addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
[allButton setTitle:@"全部" forState:UIControlStateNormal];
[allButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[allButton setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
allButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
allButton.titleLabel.font = [UIFont fontWithName: @"STHeitiSC-Medium" size: 20];
[selectionMenu addSubview: allButton];
[self.view addSubview:selectionMenu];
selectionMenu.hidden = YES;
}
-(void)showSelectionMenu {
if (selectionMenu.hidden) {
selectionMenu.hidden = NO;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect frame = selectionMenu.frame;
frame.origin.y -= frame.size.height;
selectionMenu.frame = frame;
} completion:^(BOOL finished) {
}];
}
}
-(void)hideSelectionMenu {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect frame = selectionMenu.frame;
frame.origin.y += frame.size.height;
selectionMenu.frame = frame;
} completion:^(BOOL finished) {
selectionMenu.hidden = YES;
}];
}
-(void)doSelect:(UIButton *) sender {
NSString *title = sender.titleLabel.text;
if ([title isEqualToString:@"女生"]) {
samples = @[@"2",@"3",@"4",@"5"];
}
else if ([title isEqualToString:@"男生"]) {
samples = @[@"1",@"6",@"8"];
}
else if ([title isEqualToString:@"情侶"]) {
samples = @[@"7",@"9"];
}
else if ([title isEqualToString:@"全部"]) {
samples = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
}
[self hideSelectionMenu];
[self.collectionView reloadData];
}