こんにちはUITableView、UIViewで使用するような垂直アルファベットセレクターを追加できますか?よろしくお願いします
3 に答える
3
はい。
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のインデックスセレクターに似ています
いつものように、私はバグをチェックしませんでした、そしてこれはコピー&ペーストの解決策であるべきではありません。
于 2011-02-17T07:49:43.230 に答える
1
さまざまなUILabelを使用して、それらにタグを設定できます。適切なラベルのタッチを検出するために、UITapGestureRecognizerクラスを使用できるようになりました
于 2011-02-17T08:05:09.830 に答える
0
キーパッドについて話している場合、アップルがその位置を変更することを許可していないという理由で、デフォルトの位置以外に配置することはできません。お役に立てば幸いです。ありがとうございます。
于 2011-02-17T07:44:49.753 に答える