草庐IT

ios - 长按手势弹出菜单

coder 2024-01-19 原文

我是 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

最佳答案

我现在确定你想做什么。但是我修改了你的一些代码:

@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");
            }];
    }
}

如果您需要更多帮助,请告诉我。

关于ios - 长按手势弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21792216/

有关ios - 长按手势弹出菜单的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby - Ruby 中的选项菜单 - 2

    我正在尝试在Ruby中创建一个菜单,以便根据用户输入的内容,取决于调用的类。然后在这种情况下它将返回到“Main”或类“Options”。我希望有人能帮助我。这是我的代码。modulePhysicsG=21C=20000Pi=3.14D=100endclassOptionsputs"Pleaseselect1forAccelerationand2forEnergy."option=gets()ifoption==1thenputs"AccelCalc"#ThisisthebitthatneedstodirecttheusertotheclassAccelCalcelseputs"Ene

  7. ruby - 下拉菜单在应该被选中的时候没有被选中……为什么? - 2

    我正在尝试解决我们测试中的一个错误,我认为它应该有效。我很确定这是selectize或capybara中的错误,但我不明白为什么。我已经进入了capybara的源代码,一切似乎都在正常工作。我真的不确定如何前进。为了测试这个错误,我已经尽可能地把这个错误剥离成一个小的testapplication.请参阅下面的设置bugs/show.html.erbOneTwoThreeFourOneTwoThreeFourbug_spec.rbfeature'bug'doit"specsetup",js:truedovisitbug_pathfind('div.selectize-inputinpu

  8. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  9. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  10. ruby - 如何使用 ruby​​ fibers 避免阻塞 IO - 2

    我需要将目录中的一堆文件上传到S3。由于上传所需的90%以上的时间都花在了等待http请求完成上,所以我想以某种方式同时执行其中的几个。Fibers能帮我解决这个问题吗?它们被描述为解决此类问题的一种方法,但我想不出在http调用阻塞时我可以做任何工作的任何方法。有什么方法可以在没有线程的情况下解决这个问题? 最佳答案 我没有使用1.9中的纤程,但是1.8.6中的常规线程可以解决这个问题。尝试使用队列http://ruby-doc.org/stdlib/libdoc/thread/rdoc/classes/Queue.html查看文

随机推荐