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

转载于:https://www.cnblogs.com/lijinfu-software/p/10370825.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值