我的代码遇到2个问题,我想看看是否有人可以帮我修复它?
这是我的代码:
#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)当他们在后面旋转时,我仍然可以看到旋转木马上的按钮,我将alpha设置为0,但我仍然可以看到它们?
我是Objective-c的新手,对不起我的错误,objective-c与c ++非常不同
请帮助和建议???
答案 0 :(得分: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];