[](https://travis-ci.org/Patrick Ryan/PRRadialMenuPopover)
PRRadialMenu 是从集合视图中的某一点扩展出来的按钮菜单。长按后显示菜单。菜单根据按钮数量调整间距。更多更新即将推出。
要运行示例项目,首先从仓库 Clone,然后在示例目录中运行 pod install
。
创建一个按钮数组并将其传递给 PopOverMenu。菜单将根据数组中的按钮数量进行调整。您必须为按钮的高亮和正常状态设置一个图片。以下是一种制作圆形按钮的简单方法。
CGRect frame = CGRectMake(0, 0, 44, 44);
UIButton *buttonView = [[UIButton alloc]initWithFrame:frame];
[buttonView setImage:[UIImage imageNamed:@"Image-3"] forState:UIControlStateNormal];
[buttonView setImage:[UIImage imageNamed:@"Image-2"] forState:UIControlStateHighlighted];
UIButton *buttonView2 = [[UIButton alloc]initWithFrame:frame];
[buttonView2 setImage:[UIImage imageNamed:@"Image-3"] forState:UIControlStateNormal];
[buttonView2 setImage:[UIImage imageNamed:@"Image-2"] forState:UIControlStateHighlighted];
UIButton *buttonView3 = [[UIButton alloc]initWithFrame:frame];
[buttonView3 setImage:[UIImage imageNamed:@"Image-3"] forState:UIControlStateNormal];
[buttonView3 setImage:[UIImage imageNamed:@"Image-2"] forState:UIControlStateHighlighted];
UIButton *buttonView4 = [[UIButton alloc]initWithFrame:frame];
[buttonView4 setImage:[UIImage imageNamed:@"Image-1"] forState:UIControlStateNormal];
[buttonView4 setImage:[UIImage imageNamed:@"Image"] forState:UIControlStateHighlighted];
buttonView.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor;
buttonView.layer.shadowOffset = CGSizeMake(0, 0);
buttonView.layer.shadowRadius = 2.0;
buttonView.layer.shadowOpacity = 0.4;
buttonView2.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor;
buttonView2.layer.shadowOffset = CGSizeMake(0, 0);
buttonView2.layer.shadowRadius = 2.0;
buttonView2.layer.shadowOpacity = 0.4;
buttonView3.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor;
buttonView3.layer.shadowOffset = CGSizeMake(0, 0);
buttonView3.layer.shadowRadius = 2.0;
buttonView3.layer.shadowOpacity = 0.4;
buttonView4.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor;
buttonView4.layer.shadowOffset = CGSizeMake(0, 0);
buttonView4.layer.shadowRadius = 2.0;
buttonView4.layer.shadowOpacity = 0.4;
NSArray *buttonArray = @[buttonView, buttonView2, buttonView3, buttonView4];
接下来创建 PRPopoverButtonMenu,指派代理,并添加手势识别器。
self.touchContextMenu = [[PRPopoverButtonMenu alloc] initWithFrame:self.collectionView.frame
withButtons:buttons //array of buttons
helpText:helpText //array of text to be displayed above buttons
controller:self //CollectionViewController using the menu, needed to translate coords
withOffset:offset //offset betweeen navbar and top of view. Used to detect if buttons will animate under nav bar
labelSize:CGSizeMake(60, 20) //size of labels above butons
withRadius:80 //radius from long press gesture to center of button
angleScaler:2.1 //a scaler that adjusts the angle between the buttons
initialThetaOffset:0]; //the initial offset of the arc where the buttons are placed.
//set the delegate method
self.touchContextMenu.delegate = self;
//add the gesture reconizer to the collection view
[self.collectionView addGestureRecognizer:self.touchContextMenu.longPressReconizer];
Add the two delegate methods to the collectionviewcontroller
Used this method to show the menu. Make sure there is a valid cell on the point pushed.
shouldShowAtPoint:(CGPoint)point
Used this method get the index of a pushed button. The Point returned is the point of the gesture.
didSelectButton:(NSInteger)index atPoint:(CGPoint)point
//Use this method to gurantee the location of the push was over a valid cell.
- (BOOL) buttonMenu:(PRPopoverButtonMenu*)buttonMenu shouldShowAtPoint:(CGPoint)point {
return ( [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:point]] != nil );
}
//This method is called when the button a button has been pushed.
- (void) buttonAtIndex:(PRPopoverButtonMenu *)buttonManager didSelectButton:(NSInteger)index atPoint:(CGPoint)point {
NSIndexPath *cellPath = [self.collectionView indexPathForItemAtPoint:point];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:cellPath];
if ( !cell ) {
return;
}
if ( index == 0 ) {
NSLog(@"Action 1");
} else if ( index == 1 ) {
NSLog(@"Action 2");
}else if(index == 2){
NSLog(@"Action 3");
}else {
NSLog(@"Delete");
self.cellNumber--;
[self.collectionView deleteItemsAtIndexPaths:@[cellPath]];
}
}
PRRadialMenuPopover 通过 CocoaPods 提供。要安装,只需将以下行添加到 Podfile 中
pod "PRRadialMenuPopover"
Patrick Ryan,[email protected]
PRRadialMenuPopover 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。