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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM