iPhoneアングル

タップした角度まで針を回すという感じでiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface AngleScene : SKScene

@property int angle;

@end

@implementation AngleScene

– (void)didMoveToView:(SKView *)view

{

    self.physicsWorld.gravity = CGVectorMake(0, 0);

    [self createBox];

}

– (void)createBox

{

    SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(160, 20)];

    box.name = @”box”;

    box.position = CGPointMake(220, CGRectGetMaxY(self.frame)-160);

    [self addChild:box];

    box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];

    

    SKSpriteNode *cap = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(15, 15)];

    cap.position = CGPointMake(160, box.position.y);

    [self addChild:cap];

    cap.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cap.size];

    cap.physicsBody.dynamic = NO;

    

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:box.physicsBody bodyB:cap.physicsBody anchor:cap.position];

    [self.physicsWorld addJoint:pin];

}

– (void)update:(NSTimeInterval)currentTime

{

    SKNode *box = [self childNodeWithName:@”box”];

    

    float d = 0;

    if (box.zRotation >= 0) {

        d = box.zRotation – (self.angle / 360.0) * 2.0 * M_PI;

    }

    else {

        d = 2.0*M_PI + box.zRotation – (self.angle / 360.0) * 2.0 * M_PI;

    }

    if (fabs(d) > 0.01) {

        box.physicsBody.angularVelocity = 0.2;

    } else {

        box.physicsBody.angularVelocity = 0;

    }

}

@end

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) AngleScene *scene;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    AngleScene *scene = [[AngleScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

    self.scene = scene;

    

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 50, 320) style:UITableViewStylePlain];

    table.transform = CGAffineTransformMakeRotation(-M_PI/2.0);

    table.center = CGPointMake(160, 420);

    table.delegate = self;

    table.dataSource = self;

    [self.view addSubview:table];

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 360;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @”Cell”;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

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

        cell.backgroundColor = [UIColor greenColor];

        cell.textLabel.transform = CGAffineTransformMakeRotation(M_PI/2.0);

        cell.textLabel.frame = cell.bounds;

        cell.textLabel.textAlignment = NSTextAlignmentCenter;

    }

    cell.textLabel.text = [NSString stringWithFormat:@”%d”, indexPath.row + 1];

    return cell;

}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *selected = [tableView cellForRowAtIndexPath:indexPath];

    self.scene.angle = [selected.textLabel.text intValue];

}

@end