首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用objective-c的无限旋转木马

使用objective-c的无限旋转木马
EN

Stack Overflow用户
提问于 2013-12-12 01:28:13
回答 1查看 196关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
#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++非常不同

请帮助和建议?

EN

回答 1

Stack Overflow用户

发布于 2013-12-12 02:36:52

下面这行不正确:

代码语言:javascript
复制
self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(update) userInfo:nil repeats:YES];

您正在安排一个计时器每隔0秒触发一次,这通过尝试连续执行消耗了runloop。您必须更改该行以使用大于0的值来调度计时器,例如:

代码语言:javascript
复制
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(update) userInfo:nil repeats:YES];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20525933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档