私のプログラムは、タイマー付きの画像を表示します。ユーザーが UISlider を使用してタイマーを調整できるようにしたい。
別のデリゲート「SpeedSliderDelegate」をデリゲートしてリッスンする Deck クラスがあります。Deck クラスが SpeedSliderDelegate からの変更をリッスンし、値を更新するようにします。
SpeedSliderDelegate をリッスンするように Deck.m モデルを設定するにはどうすればよいですか... (if(self = [super init] in Deck.m の直後)
SpeedSliderDelegate.h
@protocol SpeedSliderDelegate <NSObject>
-(void)setSpeed:(float)currentSpeed;
@end
Deck.h
#import "SpeedSliderDelegate.h"
#import <Foundation/Foundation.h>
#import "Card.h"
@protocol DeckLooperDelegate <NSObject>
-(void)cardHasChangedTo:card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalCount stopTimer:(NSTimer*)stopTimer cards:(NSArray *)cards;
@end
@interface Deck : NSObject <SpeedSliderDelegate>
@property (nonatomic, retain)NSMutableArray *cards;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)id <DeckLooperDelegate> delegate;
@property (nonatomic)NSInteger total;
@property (nonatomic) float testSpeed;
- (NSInteger) cardsRemaining;
- (void) startTimerLoop;
@end
Deck.m
#import "Deck.h"
#import "Card.h"
@implementation Deck
@synthesize cards;
@synthesize timer;
@synthesize delegate;
@synthesize total, testSpeed;
- (id) init
{
if(self = [super init])
{
//self.delegate = self something like this...
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int face = 1; face < 14; face++, picNum++)
{
if (face > 1 && face < 7)
aCount = 1;
else if (face > 6 && face < 10)
aCount = 0;
else
aCount = -1;
NSString* imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"card_%d", picNum] ofType:@"png"];
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
countValue:(NSInteger)aCount
suit:(Suit)suit
cardImage:(UIImage *)theImage];
[cards addObject:card];
}
}
}
return self;
}
-(void)setSpeed:(float)currentSpeed
{
testSpeed = currentSpeed;
}
-(void)startTimerLoop
{
if (!timer)
{
timer = [NSTimer scheduledTimerWithTimeInterval:testSpeed target:self
selector:@selector(timerEvent:) userInfo:nil repeats:YES ];
NSLog(@"Timer started!!!");
}
}
-(void)timerEvent:(NSTimer*)timer
{
srand(time(nil));
int index = rand()%[cards count];
Card *randomCard =[cards objectAtIndex:index];
[cards removeObjectAtIndex:index];
NSLog(@"%@" ,randomCard);
int index1 = rand()%[cards count];
Card *randomCard1 =[cards objectAtIndex:index1];
[cards removeObjectAtIndex:index1];
NSLog(@"%@" ,randomCard1);
total += (randomCard.countValue + randomCard1.countValue);
[self.delegate cardHasChangedTo:randomCard aCard2:randomCard1 totalCount:total stopTimer:self.timer cards:cards];
if ([cards count] == 0)
{
[self.timer invalidate];
self.timer = nil;
}
}
- (NSInteger) cardsRemaining
{
return [cards count];
}
- (NSString *) description
{
NSString *desc = [NSString stringWithFormat:@"Deck with %d cards\n",
[self cardsRemaining]];
return desc;
}
@end
GameViewController.h
#import "Deck.h"
#import "SpeedSliderDelegate.h"
@interface GameViewController : UIViewController <DeckLooperDelegate>
@property (nonatomic, retain) IBOutlet UIImageView *cardDisplay, *cardDisplay1;
@property (weak, nonatomic) IBOutlet UILabel *cardName, *cardName1;
@property (weak, nonatomic) IBOutlet UILabel *cardCount, *cardCount1;
@property (weak, nonatomic) IBOutlet UILabel *totalCount;
@property (nonatomic)int stop1;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UIButton *restartButton;
@property (weak, nonatomic) IBOutlet UIButton *homeButton;
@property (weak, nonatomic) IBOutlet UISlider *speed;
@property (nonatomic, retain) id <SpeedSliderDelegate> delegate;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)hideShow:(id)sender;
- (IBAction)homeAction:(id)sender;
@end
GameViewController.m
#import <QuartzCore/QuartzCore.h>
#import "GameViewController.h"
#import "Deck.h"
@implementation GameViewController
@synthesize cardDisplay, cardDisplay1, cardName, cardName1, cardCount, cardCount1, totalCount, stop1, stopButton, restartButton, homeButton, speed, delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
Deck *deck = [[Deck alloc]init];
NSLog(@"%@", deck);
for (id cards in deck.cards)
{
NSLog(@"%@", cards);
}
deck.delegate = self;
[deck startTimerLoop];
cardDisplay.layer.cornerRadius = 7;
cardDisplay.clipsToBounds = YES;
cardDisplay1.layer.cornerRadius = 7;
cardDisplay1.clipsToBounds = YES;
[restartButton setHidden:YES];
[delegate setSpeed:speed.value];
}
//#pragma mark - DeckDelegate
-(void)cardHasChangedTo:(Card *)card1 aCard2:(Card *)card2 totalCount:(NSInteger)totalC stopTimer:(NSTimer *)stopTimer cards:(NSArray *)cards
{
[self.cardDisplay setImage:card1.cardImage];
[self.cardDisplay1 setImage:card2.cardImage];
self.cardName.text = card1.description;
self.cardName1.text = card2.description;
self.cardCount.text = [NSString stringWithFormat:@"%d", card1.countValue];
self.cardCount1.text = [NSString stringWithFormat:@"%d", card2.countValue];
self.totalCount.text = [NSString stringWithFormat:@"%d", totalC];
if (stop1 == 86)
{
[stopTimer invalidate];
stopTimer = nil;
}
if ([cards count] == 0)
{
[restartButton setHidden:NO];
}
}
- (IBAction)start:(id)sender
{
stop1 = 85;
[cardDisplay setHidden:YES];
[cardDisplay1 setHidden:YES];
self.totalCount.text = @"0";
self.cardCount.text = @"0";
self.cardCount1.text = @"0";
self.cardName.text = @"";
self.cardName1.text = @"";
Deck *deck = [[Deck alloc]init];
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(cardDisplayDelay:) userInfo:nil repeats:NO];
NSLog(@"%@", deck);
deck.delegate = self;
[deck startTimerLoop];
cardDisplay.layer.cornerRadius = 7;
cardDisplay.clipsToBounds = YES;
cardDisplay1.layer.cornerRadius = 7;
cardDisplay1.clipsToBounds = YES;
[restartButton setHidden:YES];
}
- (IBAction)stop:(id)sender
{
stop1 = 86; //cancelled
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:@selector(restartButtonDelay:) userInfo:nil repeats:NO];
}
-(void)restartButtonDelay:(NSTimer*)timer
{
[restartButton setHidden:NO];
}
-(void)cardDisplayDelay:(NSTimer*)timer
{
[cardDisplay setHidden:NO];
[cardDisplay1 setHidden:NO];
}
- (IBAction)hideShow:(id)sender
{
if ([cardCount isHidden])
{
[cardCount setHidden:NO];
[cardCount1 setHidden:NO];
[totalCount setHidden:NO];
}
else
{
[cardCount setHidden:YES];
[cardCount1 setHidden:YES];
[totalCount setHidden:YES];
}
}
- (IBAction)homeAction:(id)sender
{
stop1 = 86; //cancelled
}
@end