iOS 弹出菜单UIMenuController的基本使用

UIMenuController,弹出菜单
iOS <wbr>弹出菜单UIMenuController的基本使用
@implementation DragView
{
    CGPoint startLocation;
    CGFloat rotation;
}
-(instancetype)initWithImage:(UIImage *)anImage
{
    self=[super initWithImage:anImage];
    rotation=0.0f;
    if ( self) {
        self.userInteractionEnabled=YES;
        长按手势识别器
        UILongPressGestureRecognizer *pressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
        [self addGestureRecognizer:pressGesture];
    }
    return self;
}
只有成为第一响应者时menu才会弹出
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)uilpgr
{
    if (![self becomeFirstResponder]) {
        NSLog(@"Could not become first responder");
        return;
    }
    UIMenuController *menu=[UIMenuController sharedMenuController];
    UIMenuItem *pop=[[UIMenuItem alloc]initWithTitle:@"Pop" action:@selector(popSelf)];
    UIMenuItem *rotate=[[UIMenuItem alloc]initWithTitle:@"Rotation" action:@selector(rotationSelf)];
    UIMenuItem *ghost=[[UIMenuItem alloc]initWithTitle:@"Ghost" action:@selector(ghostSelf)];
    类似于UIBarButtonItem,实例化每个UIMenuItem,然后添加到menuItems中,menuItems是个数组。
    menu.menuItems=@[pop,rotate,ghost];
   
    [menu setTargetRect:self.bounds inView:self];
    这里是这只箭头方向UIMenuControllerArrowDown就是这样iOS <wbr>弹出菜单UIMenuController的基本使用
UIMenuControllerArrowUp就是这样iOS <wbr>弹出菜单UIMenuController的基本使用同理还有left和right



    menu.arrowDirection=UIMenuControllerArrowDown;
    调用update方法才能使我们对菜单所做的修改生效
    [menu update];
    将菜单设为可见就可以了
    [menu setMenuVisible:YES];
}

- (void)popSelf
{
    [UIView animateWithDuration:0.25f animations:^(){self.transform = CGAffineTransformMakeScale(1.5f, 1.5f);} completion:^(BOOL Done){
        [UIView animateWithDuration:0.25 animations:^(){self.transform=CGAffineTransformIdentity;}] ;}];
}

- (void)rotationSelf
{
    [UIView animateWithDuration:0.25f animations:^(){self.transform = CGAffineTransformMakeRotation(rotation+M_PI * 0.5);} completion:^(BOOL done){
        rotation=M_PI*0.5+rotation;}];
}

- (void)ghostSelf
{
    [UIView animateWithDuration:1.25f animations:^(){self.alpha = 0.0f;} completion:^(BOOL done){
        [UIView animateWithDuration:1.25f animations:^(){} completion:^(BOOL done){
            [UIView animateWithDuration:0.5f animations:^(){self.alpha = 1.0f;}];
        }];
    }];
}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    startLocation = [[touches anyObject] locationInView:self];
    [self.superview bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint pt = [[touches anyObject] locationInView:self];
    float dx = pt.x - startLocation.x;
    float dy = pt.y - startLocation.y;
    CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
    
    self.center = newcenter;
}

@end

posted @ 2019-02-13 17:08  ios_福  阅读(1884)  评论(0编辑  收藏  举报