2013-12-11 41 views
0

無限旋轉木馬我面臨兩個問題我的代碼,我想看看是否有人可以幫我解決這個問題?用Objective-C的

這裏是我的代碼:

#import "ViewController.h" 

@interface ViewController() { 

@private NSInteger index; 
@private NSInteger iterations; 
@private UIButton* buttons[6]; 
@private CGPoint centers[6]; 
@private CGRect bounds[6]; 
@private CGFloat opacities[6]; 
@private CGFloat opacitieOffsets[6]; 
@private CGPoint centerOffsets[6]; 
@private CGRect boundOffsets[6]; 
@private NSString* titles[6]; 

} 

@property (strong, nonatomic) NSTimer *timer; 

@end 

@implementation ViewController 

@synthesize Button1; 
@synthesize Button2; 
@synthesize Button3; 
@synthesize Button4; 
@synthesize Button5; 
@synthesize Button6; 
@synthesize TitleLabel; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    buttons[0] = Button1; 
    titles[0] = @"btnTitle1"; 
    buttons[1] = Button2; 
    titles[1] = @"btnTitle2"; 
    buttons[2] = Button3; 
    titles[2] = @"btnTitle3"; 
    buttons[3] = Button4; 
    titles[3] = @"btnTitle4"; 
    buttons[4] = Button5; 
    titles[4] = @"btnTitle5"; 
    buttons[5] = Button6; 
    titles[5] = @"btnTitle6"; 

    for (NSInteger i = 0; i < 6; i++) 
    { 
     centers[i] = buttons[i].center; 
     bounds[i] = buttons[i].bounds; 
     opacities[i] = buttons[i].alpha; 
    } 
} 

- (void)viewDidUnload 
{ 
    [self stopAnimation]; 
    [self setButton1:nil]; 
    [self setButton2:nil]; 
    [self setButton3:nil]; 
    [self setButton4:nil]; 
    [self setButton5:nil]; 
    [self setButton6:nil]; 
    [self setTitleLabel:nil]; 

    [super viewDidUnload]; 
} 

///////////////////////////////////////////////////// 
- (IBAction)swipeLeft:(UISwipeGestureRecognizer *)sender { 
    [self stopAnimation]; 
    buttons[index].enabled = NO; 
    { 
     TitleLabel.text = @""; 
     index = [self next:index]; 
     [self startAnimation]; 
    } 
} 

- (IBAction)swipeRight:(UISwipeGestureRecognizer *)sender { 
    [self stopAnimation]; 
    buttons[index].enabled = NO; 
    { 
     TitleLabel.text = @""; 
     index = [self back:index]; 
     [self startAnimation]; 
    } 
} 
/////////////////////////////////////////////////// 
- (void)startAnimation 
{ 
    [self prepareForUpdate]; 
    [self.timer invalidate]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(update) userInfo:nil repeats:YES]; 
} 

- (void)stopAnimation 
{ 
    [self.timer invalidate]; 
    [self finalizeUpdate]; 
    self.timer = nil; 
} 

- (void)prepareForUpdate 
{ 
    NSInteger i = 0; 
    NSInteger iter = index; 

    do 
    { 
     centerOffsets[iter] = CGPointMake 
     (
     (centers[i].x - buttons[iter].center.x)/30, 
     (centers[i].y - buttons[iter].center.y)/30 
     ); 
     boundOffsets[iter] = CGRectMake 
     (
     (bounds[i].origin.x - buttons[iter].bounds.origin.x)/30, 
     (bounds[i].origin.y - buttons[iter].bounds.origin.y)/30, 
     (bounds[i].size.width - buttons[iter].bounds.size.width)/30, 
     (bounds[i].size.height - buttons[iter].bounds.size.height)/30 
     ); 
     opacitieOffsets[iter] = (opacities[i] - buttons[iter].alpha)/30; 

     i++; 
     iter = [self next:iter]; 
    } 
    while (iter != index); 
    iterations = 30; 
} 

- (void)update 
{ 
    if (iterations <= 1) 
    { 

    } 
    if (iterations > 0) 
    { 
     for (NSInteger i = 0; i < 6; i++) 
     { 
      buttons[i].center = CGPointMake 
      (
      centerOffsets[i].x + buttons[i].center.x, 
      centerOffsets[i].y + buttons[i].center.y 
      ); 
      buttons[i].bounds = CGRectMake 
      (
      boundOffsets[i].origin.x + buttons[i].bounds.origin.x, 
      boundOffsets[i].origin.y + buttons[i].bounds.origin.y, 
      boundOffsets[i].size.width + buttons[i].bounds.size.width, 
      boundOffsets[i].size.height + buttons[i].bounds.size.height 
      ); 
      buttons[i].alpha = buttons[i].alpha + opacitieOffsets[i]; 
     } 
     iterations--; 
    } 
    else [self stopAnimation]; 
} 

- (void)finalizeUpdate 
{ 
    NSInteger i = 0; 
    NSInteger iter = index; 

    do 
    { 
     buttons[iter].center = centers[i]; 
     buttons[iter].bounds = bounds[i]; 
     buttons[iter].alpha = opacities[i]; 

     i++; 
     iter = [self next:iter]; 
    } 
    while (iter != index); 
    iterations = 0; 
    buttons[index].enabled = YES; 

    TitleLabel.text = titles[index]; 
} 

- (NSInteger)back:(NSInteger) i 
{ 
    return i == 0 ? 5 : i - 1; 
} 

- (NSInteger)next:(NSInteger) i 
{ 
    return i == 5 ? 0 : i + 1; 
} 

我想說,我的代碼工作,但需要一些調整或許多可能!

1)它是傳送帶去左/右很慢的第一個問題,我更改了時間,但我還是沒有弄明白??????

2)I仍然可以看到從轉盤的按鈕時,他們在後面轉動,我設置阿爾法爲0,但我仍然可以看到他們????

我是新來的Objective-C爲我的錯誤很抱歉,Objective-C的比C++

幫助和建議請截然不同???

回答

0

以下行不正確:

self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(update) userInfo:nil repeats:YES]; 

你安排一個計時器,試圖繼續執行每0秒,這是消費runloop火災。你必須改變該行以大於0的值來安排計時器,例如:

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(update) userInfo:nil repeats:YES];