在UIView中添加垂直字母选择器

你好 我可以在UIView中添加我们在UITableView中使用的垂直字母选择器吗? 最好的祝福     
已邀请:
是。 如果你自己创建UIView,你可以做任何你想做的事。 在你的情况下,这甚至不是那么难。一些UILabel作为子视图和
touchesDidSomething:withEvent:
中的一些逻辑来确定触摸附近的标签。 还有一个委托方法,用于说明触摸了哪个部分。 我想我可能需要这样的东西,所以我决定尝试一下。
//myIndexSelectorView.m

- (id)initWithFrame:(CGRect)frame andLabels:(NSArray *)l {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor lightGrayColor];
        self.layer.cornerRadius = frame.size.width/2;
        labels = [l retain];
        NSInteger count;
        for (NSString *string in labels) {
            CGFloat margin = 5.0f;
            CGFloat yPosition = count*(frame.size.height/[labels count]);
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(margin, yPosition, frame.size.width-2*margin, frame.size.height/[labels count])] autorelease];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor darkGrayColor];
            label.textAlignment = UITextAlignmentCenter;
            label.text = string;
            [self addSubview:label];
            count++;
        }
    }
    return self;
}

- (void)touch:(UITouch *)touch {
    CGPoint touchPosition = [touch locationInView:self];
    NSInteger index = touchPosition.y / (self.bounds.size.height / [labels count]);
    NSLog(@"Touched: %@", [labels objectAtIndex:index]);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self touch:touch];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self touch:touch];
}

- (void)dealloc {
    [labels release];
    [super dealloc];
}
按预期工作,看起来类似于uitableview的索引选择器 像往常一样,我没有检查错误,这不应该是一个复制和粘贴解决方案。     
您可以使用不同的UILabel,并为它们设置标签。现在要检测适当标签上的触摸,您可以使用UITapGestureRecognizer类     
如果你在谈论键盘,那么除了默认位置之外不能定位苹果不允许改变其位置的原因。希望对你有所帮助。谢谢。     

要回复问题请先登录注册