【问题标题】:Adding a tap gesture to a UIImageView to change a UILabel?向 UIImageView 添加轻击手势以更改 UILabel?
【发布时间】:2014-12-30 21:01:34
【问题描述】:

我已经在 Xcode 6 中开发 iOS 单视图应用程序几天了,当我尝试添加触摸事件时遇到了障碍。这是我想要做的:

在情节提要中,我创建了一个 UILabel 和一个 UIImageView,我想向 UIImageView 添加一个点击手势,以便当我点击图像时,标签会发生变化。虽然我在 Objective-C 中编程不是很舒服,但我知道我必须首先将手势附加到 UIImageView,然后可以由我的事件处理程序方法处理。这是我尝试过的:

在 ViewController.h 文件中:

   #import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UIImageView *imageView;
    IBOutlet UILabel *myLabel;

}

-(void)handleTap:(id)sender;

@end

在 ViewController.m 文件中:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {

imageView.userInteractionEnabled = YES;

UITapGestureRecognizer *pgr = [[UITapGestureRecognizer alloc]
                                 initWithTarget:self
                                         action:@selector(handleTap)];

[imageView addGestureRecognizer:pgr];

    [super viewDidLoad];
}

- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer
{
    myLabel.text = [NSString stringWithFormat:@"It Worked"];
}

我预期会发生的是,当点击 imageView 时 myLabel 会发生变化,myLabel 和 imageView 分别链接到 UILabel 和 UIImageView。而且我确实意识到,只需向 imageView 添加一个按钮,我可能会为自己省去很多麻烦,但我真的在努力让它与触摸事件一起工作。

我是这门语言的新手,这是我第一次尝试构建一些东西。我在发布此内容时已尝试尽可能详细,但如果您需要更多详细信息,我很乐意提供。非常感谢详细信息、代码示例和对各个部分如何组合在一起的解释。谢谢。

【问题讨论】:

  • 您是否将插座连接到 Interface Builder 中的标签?
  • 另外...您需要将这些 IBOutlets 创建为“@properties”,然后在您的实现中“@synthesize”它们。
  • @ElGuapo 不需要,但这是最佳实践。 @OP 如果你在 handleTap: 方法中设置一个断点,它会被命中吗?
  • 您的操作应该是 handleTap:,而不是 handleTap。并且没有必要综合你的属性。它不再被认为是好的做法。
  • 这是什么时候发生的??我想我需要看一些 WWDC ......感谢您的澄清

标签: ios objective-c uiimageview uilabel uitapgesturerecognizer


【解决方案1】:
#import "ViewController.m"

@interface ViewController ()
{
    IBOutlet UILabel *myLabel;
    IBOutlet UIImageView *myImage;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tapped:)];
    tap.numberOfTapsRequired = 1; //You can change this is double tap ect..
    Red.cancelsTouchesInView = YES;
    myImage.userInteractionEnabled = YES; //if you want touch on your image you'll need this
    [myImage addGestureRecognizer:tap];
}

-(void)Tapped:(UITapGestureRecognizer *)sender
{
    myLabel.text = [NSString stringWithFormat:@"It Worked"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

如果您的游戏仍然无法运行,则说明您尚未将标签和图像连接到界面构建器。要将它们连接到界面构建器,请将 do 变暗并将其连接到视图上的所需对象,如图所示,我单击点并将其拖到标签上。

【讨论】:

    【解决方案2】:

    您缺少 ':' 登录手势选择器。它应该是这样的:

    ... action:@selector(handleTap:)];
    

    如果仍然无法正常工作,那么您的IBOutlet 连接可能是错误的。如果操作正确,您应该会在 Xcode 中看到带有点的小圆圈。 Check this sample image:

    【讨论】:

    • 我确实错过了。在我离开它几个小时后我重新做了它,它似乎工作正常。我的工作版本确实包含了您所包含的正确语法,感谢您的关注。
    猜你喜欢
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多