iPhoneボタン長押し

左右ボタンを長押しで真ん中をグルグルするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic) int index;

@property (nonatomic, weak) UIView *r;

@property (nonatomic, weak) UIView *l;

@property (nonatomic, strong) NSMutableArray *btns;

@property (nonatomic, weak) NSTimer *pressTimer;

@property BOOL moving;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor yellowColor];

    

    [self createCursor];

    [self createDefaultButton];

}

– (void)createCursor

{

    UILabel *r = [[UILabel alloc] init];

    r.text = @”>”;

    r.font = [UIFont systemFontOfSize:50];

    r.textColor = [UIColor greenColor];

    [r sizeToFit];

    r.center = CGPointMake(CGRectGetMaxX(self.view.bounds) – 40, CGRectGetMidY(self.view.bounds));

    [self.view addSubview:r];

    self.r = r;

    

    

    UILabel *l = [[UILabel alloc] init];

    l.text = @”<“;

    l.font = [UIFont systemFontOfSize:50];

    l.textColor = [UIColor greenColor];

    [l sizeToFit];

    l.center = CGPointMake(40, CGRectGetMidY(self.view.bounds));

    [self.view addSubview:l];

    self.l = l;

}

– (void)createDefaultButton

{

    self.btns = [NSMutableArray array];

    

    for (int i=0; i<3; i++) {

        float x = i * 60 + 100;

        float y = CGRectGetMidY(self.view.bounds);

        

        UIButton *b = [self createButton:[@(i) stringValue]];

        b.center = CGPointMake(x, y + 5);

        [self.btns addObject:b];

    }

}

– (UIButton *)createButton:(NSString *)title

{

    

    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];

    b.frame = CGRectMake(0, 0, 40, 40);

    b.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];

    b.layer.cornerRadius = 20;

    

    [self.view addSubview:b];

    

    [b setTitle:title forState:UIControlStateNormal];

    b.titleLabel.font = [UIFont boldSystemFontOfSize:20];

    [b setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

    

    return b;

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    SEL tap;

    if (CGRectContainsPoint(self.r.frame, p)) {

        [self tapR];

        tap = @selector(tapR);

    } else if (CGRectContainsPoint(self.l.frame, p)) {

        [self tapL];

        tap = @selector(tapL);

    }

    

    self.pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:tap userInfo:nil repeats:YES];

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.pressTimer invalidate];

    self.pressTimer = nil;

}

– (void)tapR

{

    if (self.moving) return;

    

    self.moving = YES;

    

    // setup new array

    NSMutableArray *newArr = [NSMutableArray array];

    UIView *newV = [[UIView alloc] init];

    [newArr addObject:newV];

    [newArr addObject:self.btns[0]];

    [newArr addObject:self.btns[1]];

    

    

    // animation

    for (int i=0; i<self.btns.count; i++) {

        UIButton *v = (UIButton *)self.btns[i];

        [UIView animateWithDuration:0.3 animations:^{

            if (i != 2) {

                v.center = CGPointMake(v.center.x + 60, v.center.y);

            } else {

                v.alpha = 0.3;

            }

        } completion:^(BOOL finished) {

            if (i == 0) {

                // create

                UIView *b = [self createButton:[NSString stringWithFormat:@”%d”, [v.titleLabel.text intValue] – 1]];

                b.center = CGPointMake(v.center.x60, v.center.y);

                newArr[0] = b;

            } else if (i == 2) {

                [UIView animateWithDuration:0.2 animations:^{

                    v.alpha = 0;

                } completion:^(BOOL finished) {

                    [v removeFromSuperview];

                }];

            }

        }];

    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        self.moving = NO;

        self.btns = newArr;

    });

}

– (void)tapL

{

    if (self.moving) return;

    

    self.moving = YES;

    

    // setup new array

    NSMutableArray *newArr = [NSMutableArray array];

    UIView *newV = [[UIView alloc] init];

    [newArr addObject:self.btns[1]];

    [newArr addObject:self.btns[2]];

    [newArr addObject:newV];

    

    

    // animation

    for (int i=0; i<self.btns.count; i++) {

        UIButton *v = (UIButton *)self.btns[i];

        [UIView animateWithDuration:0.3 animations:^{

            if (i != 0) {

                v.center = CGPointMake(v.center.x60, v.center.y);

            } else {

                v.alpha = 0.3;

            }

        } completion:^(BOOL finished) {

            if (i == 2) {

                // create

                UIView *b = [self createButton:[NSString stringWithFormat:@”%d”, [v.titleLabel.text intValue] + 1]];

                b.center = CGPointMake(v.center.x + 60, v.center.y);

                newArr[2] = b;

            } else if (i == 0) {

                [UIView animateWithDuration:0.2 animations:^{

                    v.alpha = 0;

                } completion:^(BOOL finished) {

                    [v removeFromSuperview];

                }];

            }

        }];

    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        self.moving = NO;

        self.btns = newArr;

    });

}

@end