わかりましたので、スクロールが行われるように、いくつかを作成してそれらにデータを入力するというカスタムUIView
クラスがあります。ただし、のプロパティに適切にアクセスできません。NumberTicker
UIScrollViews
UILabels
NumberTicker
scrollviews
[self.scrollview setHidden:YES];
// or
[self.scrollview setContentOffset:offset];
上記、および実際には のプロパティを設定または取得しようとする試みは、が として追加される前に が作成されUIScrollView
た場合を除き、機能しません。scrollview
subview
これが私のNumberTicker.h
ファイルです:
// NumberTicker.h
#import <Foundation/Foundation.h>
#import "ProfileViewController.h"
@interface NumberTicker : UIScrollView <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *digitOne;
@property (weak, nonatomic) IBOutlet UIScrollView *digitTwo;
@property (weak, nonatomic) IBOutlet UIScrollView *digitThree;
@property (weak, nonatomic) IBOutlet UIScrollView *digitFour;
@property (weak, nonatomic) IBOutlet UIScrollView *digitFive;
@property (weak, nonatomic) IBOutlet UIScrollView *digitSix;
@property (weak, nonatomic) IBOutlet UIScrollView *digitSeven;
@property (weak, nonatomic) IBOutlet UIScrollView *digitEight;
- (id) initWithDigits:(int)digits andFrame:(CGRect)frame;
- (void) setNumberTickerTo:(int)number;
@end
と NumberTicker.m:
// NumberTicker.m
#import "NumberTicker.h"
@implementation NumberTicker
- (id) initWithDigits:(int)digits andFrame:(CGRect)frame
{
self = [super init];
if (self) {
// add digits
[self setDigitEight:_digitEight];
[self setDigitOne:_digitOne];
if (digits > 6) {
// make 8 digit ticker
[self setFrame:frame];
for (int i = 1; i <= digits; i++) {
switch (i) {
case 1:
[self addSmallScrollView:_digitOne withXPos:(frame.size.width - (i*30))];
break;
case 2:
[self addSmallScrollView:_digitTwo withXPos:(frame.size.width - (i*30))];
break;
case 3:
[self addSmallScrollView:_digitThree withXPos:(frame.size.width - (i*30))];
break;
case 4:
[self addSmallScrollView:_digitFour withXPos:(frame.size.width - (i*30))];
break;
case 5:
[self addSmallScrollView:_digitFive withXPos:(frame.size.width - (i*30))];
break;
case 6:
[self addSmallScrollView:_digitSix withXPos:(frame.size.width - (i*30))];
break;
case 7:
[self addSmallScrollView:_digitSeven withXPos:(frame.size.width - (i*30))];
break;
case 8:
[self addSmallScrollView:_digitEight withXPos:(frame.size.width - (i*30))];
break;
default:
break;
}
}
} else {
// make 6 digit ticker
[self setFrame:frame];
for (int i = 1; i <= digits; i++) {
switch (i) {
case 1:
[self addScrollView:_digitOne withXPos:(frame.size.width - (i*40))];
break;
case 2:
[self addScrollView:_digitTwo withXPos:(frame.size.width - (i*40))];
break;
case 3:
[self addScrollView:_digitThree withXPos:(frame.size.width - (i*40))];
break;
case 4:
[self addScrollView:_digitFour withXPos:(frame.size.width - (i*40))];
break;
case 5:
[self addScrollView:_digitFive withXPos:(frame.size.width - (i*40))];
break;
case 6:
[self addScrollView:_digitSix withXPos:(frame.size.width - (i*40))];
break;
default:
break;
}
}
}
}
return self;
}
- (void) addScrollView:(UIScrollView *)scrollView withXPos:(int)xPos
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xPos, 0, 40, 60)];
scrollView.contentSize = CGSizeMake(40, 660);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.userInteractionEnabled = NO;
// All of these properties are set properly, however I can't access them again in another method or class, after the `scrollview` has been added as a `subview`
for (int i = 0; i <= 10; i++) {
// create and add labels to scrollview
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*60, 40, 60)];
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:70]];
[label setTextAlignment:NSTextAlignmentCenter];
if (i == 10) {
[label setText:@"0"];
} else {
[label setText:[NSString stringWithFormat:@"%i", i]];
}
[scrollView addSubview:label];
}
[self addSubview:scrollView];
}
- (void) addSmallScrollView:(UIScrollView *)scrollView withXPos:(int)xPos
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xPos, 0, 30, 50)];
scrollView.contentSize = CGSizeMake(30, 550);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.userInteractionEnabled = NO;
scrollView.delegate = self;
for (int i = 0; i <= 10; i++) {
// create and add labels to scrollview
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*50, 30, 50)];
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:50]];
[label setTextAlignment:NSTextAlignmentCenter];
if (i == 10) {
[label setText:@"0"];
} else {
[label setText:[NSString stringWithFormat:@"%i", i]];
}
[scrollView addSubview:label];
}
[self addSubview:scrollView];
}
- (void) setNumberTickerTo:(int)number
{
NSMutableArray *digitsArray = [[NSMutableArray alloc] init];
while (number > 0) {
[digitsArray addObject:[NSNumber numberWithInt:(number % 10)]];
number = number / 10;
}
for (int i = 1; i <= [digitsArray count]; i++) {
switch (i) {
case 1:
[self.digitOne setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES]; // this doesn't work
NSLog(@"%@", self.digitOne);
[digitsArray removeObjectAtIndex:0];
break;
case 2:
[_digitTwo setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 3:
[_digitThree setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 4:
[_digitFour setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 5:
[_digitFive setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 6:
[_digitSix setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 7:
[_digitSeven setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
case 8:
[_digitEight setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
[digitsArray removeObjectAtIndex:0];
break;
default:
break;
}
}
}
@end
次に、メソッドNumberTicker
から呼び出します。ViewController.m
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NumberTicker *numberTicker = [[NumberTicker alloc] initWithDigits:3 andFrame:CGRectMake(20, 20, 240, 60)];
[self.view addSubview:numberTicker];
[numberTicker setNumberTickerTo:345];
}
と同様にNumberTicker
ビューが画面に表示されscrollviews
、すべて問題ないようです。ただし、を非表示にしたりscrollviews
、setContentOffset
スクロールしたり、アニメーション化したりすることはできません。
プロパティを合成し、デリゲートを設定し、IB を使用してビューを作成し、アウトレットを適切に接続しようとしました (この特定のクラスの IB に興味がないため、これを削除しましたが、いずれにしても機能しませんでした)。 .
私が試したら
NSLog(@"%@", self.digitOne);
コンソールには、self.digitOne が (null) として表示されます。
私は何を間違っていますか?後で他のメソッドで編集可能なプロパティを使用して、適切にサブクラス化UIView
し、プログラムで作成するにはどうすればよいですか?UIScrollViews