2014-02-15 155 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