带objective-c的无限旋转木马

带objective-c的无限旋转木马,objective-c,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 CGFlo

我的代码有两个问题,我想看看是否有人能帮我解决

这是我的密码:

#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;
}
#导入“ViewController.h”
@界面视图控制器(){
@私有NSInteger指数;
@私有NSInteger迭代;
@专用UIButton*按钮[6];
@私人CGPoint中心[6];
@私有CGRect界限[6];
@私人股本市场[6];
@私人CGOPACITIEOFFSET[6];
@专用CGPoint中心偏移量[6];
@私有CGRect边界偏移量[6];
@私有NSString*标题[6];
}
@属性(强,非原子)NSTimer*计时器;
@结束
@实现视图控制器
@合成按钮1;
@合成按钮2;
@合成按钮3;
@合成按钮4;
@合成按钮5;
@合成按钮6;
@合成滴定标记;
-(无效)viewDidLoad
{
[超级视图下载];
按钮[0]=按钮1;
标题[0]=“btnTitle1”;
按钮[1]=按钮2;
标题[1]=“btnTitle2”;
按钮[2]=按钮3;
标题[2]=“btnTitle3”;
按钮[3]=按钮4;
标题[3]=“btnTitle4”;
按钮[4]=按钮5;
标题[4]=“btnTitle5”;
按钮[5]=按钮6;
标题[5]=“btnTitle6”;
对于(NSInteger i=0;i<6;i++)
{
中心[i]=按钮[i]。中心;
边界[i]=按钮[i]。边界;
不透明度[i]=按钮[i].alpha;
}
}
-(无效)视图卸载
{
[自动停止动画];
[自我挫折1:无];
[自我挫折2:无];
[自我挫折3:无];
[自我挫折4:无];
[自我挫折5:无];
[自我挫折6:无];
[自设置标题标签:无];
[超级视频下载];
}
/////////////////////////////////////////////////////
-(iAction)swipeLeft:(UISwipegestureRecognitor*)发送方{
[自动停止动画];
按钮[索引]。已启用=否;
{
TitleLabel.text=@”;
索引=[self-next:index];
[自我启动];
}
}
-(iAction)SwiperRight:(UISwipegestureRecognitor*)发送方{
[自动停止动画];
按钮[索引]。已启用=否;
{
TitleLabel.text=@”;
索引=[自回:索引];
[自我启动];
}
}
///////////////////////////////////////////////////
-(无效)startAnimation
{
[自行准备更新];
[自动定时器失效];
self.timer=[NSTimer scheduledTimerWithTimeInterval:0目标:自选择器:@selector(update)userInfo:nil repeats:YES];
}
-(void)停止动画
{
[自动定时器失效];
[自我更新];
self.timer=nil;
}
-(无效)准备更新日期
{
NSInteger i=0;
NSInteger-iter=指数;
做
{
中心偏移量[iter]=CGPointMake
(
(centers[i].x-按钮[iter].center.x)/30,,
(中心[i].y-按钮[iter].center.y)/30
);
边界偏移量[iter]=CGRectMake
(
(bounds[i].origin.x-按钮[iter].bounds.origin.x)/30,
(bounds[i].origin.y-按钮[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
);
不透明度偏移量[iter]=(不透明度[i]-按钮[iter].alpha)/30;
i++;
iter=[自我下一步:iter];
}
while(iter!=指数);
迭代次数=30次;
}
-(作废)更新
{
如果(迭代次数0)
{
对于(NSInteger i=0;i<6;i++)
{
按钮[i]。中心=CGPointMake
(
中心偏移量[i].x+按钮[i].center.x,
中心偏移量[i].y+按钮[i].center.y
);
按钮[i].bounds=CGRectMake
(
BoundOffset[i].origin.x+按钮[i].bounds.origin.x,
BoundOffset[i].origin.y+按钮[i].bounds.origin.y,
BoundOffset[i].size.width+按钮[i].bounds.size.width,
BoundOffset[i].size.height+按钮[i].bounds.size.height
);
按钮[i].alpha=按钮[i].alpha+opacitieOffsets[i];
}
迭代--;
}
else[自动停止动画];
}
-(无效)最终确定更新
{
NSInteger i=0;
NSInteger-iter=指数;
做
{
按钮[iter]。中心=中心[i];
按钮[iter].bounds=bounds[i];
按钮[iter].alpha=不透明度[i];
i++;
iter=[自我下一步:iter];
}
while(iter!=指数);
迭代次数=0;
按钮[索引]。已启用=是;
TitleLabel.text=标题[索引];
}
-(NSInteger)背:(NSInteger)我
{
返回i==0?5:i-1;
}
-下一个:(下一个)我
{
返回i==5?0:i+1;
}
我想说,我的代码工作,但需要一些调整或许多可能

1) 第一个问题是旋转木马向左/向右走得很慢,我改变了时间,但我仍然没有弄清楚

2) 我仍然可以看到旋转木马上的按钮,当它们在后面旋转时,我将alpha设置为0,但我仍然可以看到它们

我是objective-c的新手,很抱歉我犯了错误,objective-c和c非常不同++


请提供帮助和建议???

以下行不正确:

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];