2014-02-15 48 views
1

我是新来的IOS和核心动画,所以我正在做一些测试,以适应它。 我有一个菜单,我试图通过做一个长按手势来创建。我希望按钮可以通过动画制作,然后制作动画,然后消失。我看到部分工作,但我无法弄清楚如何使它消失。它也不会让我在我的UIGestureRecognizerStateEnded语句中动画我的imageView。基本上,我有两个问题:长按菜单弹出菜单

  1. 当长按被释放后,我怎样才能使按钮动起来?

  2. 我怎样才能使这些按钮中的一个,而不是他们每次出现我长按?

下面是我在我的.m

-(void)onPress:(UILongPressGestureRecognizer*)longpress{ 

CGPoint touchCenter = [longpress locationInView:self.view]; 

UIImage *plus = [UIImage imageNamed:@"plus"]; 
imageView = [[UIImageView alloc]initWithImage:plus]; 

CGRect imageViewFrame = CGRectMake(0, 0, 35, 35); 
imageViewFrame.origin = CGPointMake(touchCenter.x - imageViewFrame.size.width/2.0f, touchCenter.y - imageViewFrame.size.height/2.0f); 
imageView.frame = imageViewFrame; 

if (longpress.state == UIGestureRecognizerStateBegan) { 
    [self.view addSubview:imageView]; 

    [UIView animateWithDuration:0.6 animations:^{ 
     CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, -40); 
     imageView.transform = moveTrans; 
    }]; 
    NSLog(@"Long press"); 
    return; 
}else{ 


if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) { 

    [UIView animateWithDuration:0.6 animations:^{ 
     CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, 40); 
     imageView.transform = moveTrans; 
     NSLog(@"long press done"); 
    }]; 

} 


} 

}

和我的.h

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
{ 
    UIImageView *imageView; 
    CAShapeLayer *layer; 

} 
@property(nonatomic) float angle; 
@property(nonatomic, strong) UIImageView *imageView; 
@property (nonatomic, strong) UIImage *image; 
@end 

回答

1

我现在相信你想要做什么。但我修改了一些你的代码:

@interface ViewController() 
@property (strong, nonatomic) UIView *moveView; 
@end 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UILongPressGestureRecognizer *recoginzer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)]; 
    [self.view addGestureRecognizer:recoginzer]; 
} 

- (void)onPress:(UILongPressGestureRecognizer*)longpress { 
    CGPoint location = [longpress locationInView:self.view]; 
    if (longpress.state == UIGestureRecognizerStateBegan) { 
     if (!self.moveView) { 
      self.moveView = [[UIView alloc] initWithFrame:CGRectMake(location.x, location.y, 100, 100)]; 
      self.moveView.backgroundColor = [UIColor redColor]; 
      [self.view addSubview:self.moveView]; 
     } else { 
      self.moveView.frame = CGRectMake(location.x, location.y, 100, 100); 
     } 
     [UIView animateWithDuration:0.6 animations:^{ 
      CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, -40); 
      self.moveView.transform = moveTrans; 
     }]; 
     NSLog(@"Long press"); 
    } else if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) { 
      [UIView animateWithDuration:0.6 animations:^{ 
       CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, 40); 
       self.moveView.transform = moveTrans; 
       NSLog(@"long press done"); 
      }]; 
    } 
} 

让我知道你是否需要更多的帮助。

+0

这就是我正在寻找的行为,但我会如何将它应用于uibutton而不是uiview? – Steven07

+1

@StevenP。只需将该按钮添加到该视图。 – lancy