我对 ios 和核心动画都很陌生,所以我正在做一些测试来适应它。
我有一个菜单,我试图通过长按手势来创建。我希望按钮先向上动画,然后向下动画,然后消失。我让出现的部分工作了,但我不知道如何让它消失。它也不允许我在 UIGestureRecognizerStateEnded 语句中对 imageView 进行动画处理。基本上,我有两个问题:
如何在长按释放时使按钮向下移动?
我怎样才能只让这些按钮之一而不是每次长按时出现它们?
这是我的 .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
我现在确定你想做什么。但我修改了你的一些代码:
@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");
}];
}
}
如果您需要更多帮助,请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)