【问题标题】:Cant see UIImageView property inside touchesBegan在 touchesBegan 中看不到 UIImageView 属性
【发布时间】:2010-12-31 16:02:02
【问题描述】:

我有一个非常简单的应用程序,但我刚开始就卡住了。 让我在这里发布代码。

TapStackView.h

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

@interface TapStackView : UIView
{
    //Layers.
    UIImageView *backgroundImageView;
    UIImageView *powerOn;
}
@property (nonatomic, retain) UIImageView *backgroundImageView;
@property (nonatomic, retain) UIImageView *powerOn;
@end

TapStackView.m

#import "TapStackView.h"

@implementation TapStackView
@synthesize backgroundImageView, powerOn;

//Initialize,
-(id)initWithFrame: (CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //Set up layers.
        self.backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"layout.png"]];

        self.powerOn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"powerOn.png"]];     
        self.powerOn.center = CGPointMake(159 + powerOn.frame.size.width/2, 26 + powerOn.frame.size.height/2);

        //Attach to the view.
        [self addSubview:self.backgroundImageView];
        [self addSubview:powerOn];      

        NSLog(@"initWithFrame - powerOn.center is %f, %f", self.powerOn.center.x, self.powerOn.center.y);   

    }
    return self;
}

//Finger touched.
-(void)touchesBegan: (NSSet*)touches withEvent: (UIEvent*)event
{
    NSLog(@"-----");
    NSLog(@"touchesBegan - self.powerOn.center is %f, %f", self.powerOn.center.x, self.powerOn.center.y);   
}

@end

控制台

[Session started at 2010-01-01 21:39:42 +0100.]
2010-01-01 21:39:44.177 TapStack[6261:20b] initWithFrame - powerOn.center is 186.000000, 53.000000
2010-01-01 21:39:45.149 TapStack[6261:20b] -----
2010-01-01 21:39:45.150 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:45.978 TapStack[6261:20b] -----
2010-01-01 21:39:45.979 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:48.914 TapStack[6261:20b] -----
2010-01-01 21:39:48.915 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:50.011 TapStack[6261:20b] -----
2010-01-01 21:39:50.012 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000

我无法想象为什么我在 touchesBegan 方法中看不到 powerOn 属性。 有什么想法吗?

【问题讨论】:

    标签: iphone uiview properties uiimageview


    【解决方案1】:

    您需要在视图上设置 .userInteractionEnabled:

    myView.userInteractionEnabled = YES;

    来自文档:新的图像视图对象默认配置为忽略用户事件。如果要在 UIImageView 的自定义子类中处理事件,则必须在初始化对象后将 userInteractionEnabled 属性的值显式更改为 YES。

    http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html

    我还认为您可能需要继承 UIImageView 并将其用于您的成员变量,而不是直接的 UIImageView。如果你想检测 UIImageView 上的触摸,你应该为 UIImageView 子类设置 touchesBegan 委托方法,而不是你的 UIView 子类。

    不过,您可以检测 UIView 中的触摸,然后使用 CGRectContains 来查看它们是否触摸了适当的 UIImageView

    【讨论】:

    • 我尝试了 userInteractionEnabled ,结果相同。子类化听起来很有希望。
    【解决方案2】:

    注意:我假设 UIImageView 子视图正确显示并且它们确实具有非零中心。

    这种情况很少见,但有时点符号无法正确解析。尝试旧的嵌套样式参考。

    NSLog(@"touchesBegan - self.powerOn.center is %@",NSStringFromCGPoint([[self powerOn] center]);
    

    如果这可行,那么您可能遇到了链接问题。检查您构建的结果中是否存在与 UIImageViews 相关的任何警告。有时链接器似乎失去了对某些嵌套类的跟踪。一个干净的一切通常可以解决问题。

    【讨论】:

      【解决方案3】:

      我无法重现此内容。你如何将 TapStackView 添加到它的超级视图中?从基于 Window 的模板中,我刚刚将其添加到 applicationDidFinishLaunching:

          [window addSubview:[[[TapStackView alloc] initWithFrame:[window bounds]] autorelease]];
      

      它似乎按您的预期工作:

      2010-01-01 17:23:00.873 Untitled[3708:207] initWithFrame - powerOn.center is 215.000000, 90.000000
      2010-01-01 17:23:02.940 Untitled[3708:207] -----
      2010-01-01 17:23:02.941 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
      2010-01-01 17:23:11.199 Untitled[3708:207] -----
      2010-01-01 17:23:11.200 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
      2010-01-01 17:23:11.663 Untitled[3708:207] -----
      2010-01-01 17:23:11.664 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
      2010-01-01 17:23:11.665 Untitled[3708:207] -----
      2010-01-01 17:23:11.665 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
      

      顺便说一句,您同时泄露了backgroundImageViewpowerOn

      【讨论】:

      • 是的,看起来没问题。也许问题出在 TapStackView 之上。
      • 嘿,谢谢你的证明。给了我足够的勇气摆脱不必要的 viewController 类。只是按照它的方式工作。再次感谢您的测试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2022-10-15
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多