2

这是我的问题:

我有一个扩展自的类,UIView我调用了VerticalFieldLayout.

字段布局标题:

#import <Foundation/Foundation.h>
#import "Layout.h"

#import <QuartzCore/QuartzCore.h>
static int const VIEW_SPACE = 5;
extern int HORIZONTAL_LAYOUT;
extern int VERTICAL_LAYOUT;

@interface FieldLayout : UIView <Layout> {
    @private
    float xPos;
    float yPos;
    int type;
}
@property(nonatomic) int type;

-(void) applyRadius: (float) radius;
@end

现场布局实施

#import "FieldLayout.h"

int HORIZONTAL_LAYOUT = 345678;
int VERTICAL_LAYOUT = 12345;

@implementation FieldLayout
@synthesize type;

- (id)init {
    self = [super init];
    if (self) {
        xPos = 0;
        yPos = 0;
    }

    return self;
}

- (void)resize:(CGRect)movement {
    float cwidth = self.frame.size.width;
    float cheight = self.frame.size.height;

    if (type == HORIZONTAL_LAYOUT) {
        if (cwidth < movement.size.width + movement.origin.x) {
            cwidth = movement.size.width + movement.origin.x;
        }
    }

    if (type == VERTICAL_LAYOUT) {
        if (cheight < movement.size.width + movement.origin.y) {
            cheight = movement.size.height + movement.origin.y;
        }
    }

    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, cwidth, cheight);
}

- (void)applyRadius:(float)radius {
    [[self layer] setCornerRadius:radius];
    [[self layer] setMasksToBounds:YES];
}

- (void)addSubview:(UIView *)view {
    if (type == HORIZONTAL_LAYOUT) {
        view.frame = CGRectMake(xPos, view.frame.origin.y, view.frame.size.width, view    .frame.size.height);

        xPos = xPos + view.frame.size.width + VIEW_SPACE;
    } else {
        view.frame = CGRectMake(view.frame.origin.x, yPos, view.frame.size.width,     view.frame.size.height);

        yPos = yPos + view.frame.size.height + VIEW_SPACE;
    }

    [self resize:view.frame];

    [super addSubview:view];
}


@end

垂直标题:

#import <Foundation/Foundation.h>
#import "FieldLayout.h"
#import <QuartzCore/QuartzCore.h>

@interface VerticalFieldLayout : FieldLayout
    - (void)setBorderNcolor:(float)width andcolor:(UIColor *)andcolor;
@end

垂直实施:

#import "VerticalFieldLayout.h"

@implementation VerticalFieldLayout

- (id)init {
    self = [super init];
    if (self) {
        [self setType:VERTICAL_LAYOUT];

        self.userInteractionEnabled = YES;
    }

    return self;
}

- (void)applyRadius:(float)radius {
    [[self layer] setCornerRadius:radius];
}

- (void)setBorderNcolor:(float)width andcolor:(UIColor *) color {
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
}
@end

现在,我使用 addSubviews 将 aprox 4 VerticalLayouts 添加到 VerticalFieldLayout。在每个 VerticalFieldLayout(4 个)中,我添加了 UIExfields。

问题: 为什么当我在 UITextfields 上选项卡时选项卡事件没有响应?但是如果我将 UITextfields 直接添加到 UIViewcontroller 的视图中,它们就可以完美地工作。

4

1 回答 1

0

我得到了解决方案:

我不得不在 FieldLayout 中重写这个方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSEnumerator *reverseE = [self.subviews reverseObjectEnumerator];
    UIView *iSubView;

    while ((iSubView = [reverseE nextObject])) {
        UIView *viewWasHit = [iSubView hitTest:[self convertPoint:point toView:iSubView]     withEvent:event];
        if (viewWasHit) {
            return viewWasHit;
        }

    }
    return [super hitTest:point withEvent:event];
}

现在,每个 SubView 都响应触摸事件!

于 2012-10-02T13:34:13.303 回答