
タップでエビングハウス錯視(ebbinghaus illusion)っぽいを体験するようなiPhoneアプリのサンプルコードを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
@interface SatelliteItem : NSObject <UIDynamicItem>
@property (nonatomic, strong) UIView *target;
@property CGPoint o;
– (instancetype)initWithTarget:(UIView *)target;
@end
@implementation SatelliteItem
– (instancetype)initWithTarget:(UIView *)target
{
self = [super init];
if (self)
{
_target = target;
_o = target.center;
}
return self;
}
– (CGRect)bounds
{
return self.target.bounds;
}
– (CGPoint)center
{
return CGPointMake(self.target.bounds.size.width, self.target.bounds.size.height);
}
– (void)setCenter:(CGPoint)center
{
float angle = (self.target.tag – 1) * M_PI / 3.0;
float l = center.x – 30;
float dx = l * cos(angle);
float dy = l * sin(angle);
self.target.center = CGPointMake(self.o.x + dx, self.o.y + dy);
self.target.bounds = CGRectMake(0, 0, center.x, center.y);
self.target.layer.cornerRadius = center.x/2.0;
}
– (CGAffineTransform)transform
{
return self.target.transform;
}
– (void)setTransform:(CGAffineTransform)transform
{
self.target.transform = transform;
}
@end
@interface ViewController ()
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[self createBalls];
[self copyScreen];
[self createLabel];
}
– (void)createBalls
{
CGSize s = CGSizeMake(30, 30);
CGPoint o = CGPointMake(CGRectGetMaxX(self.view.bounds)/4.0, CGRectGetMidY(self.view.bounds));
UIView *planet = [[UIView alloc] initWithFrame:CGRectMake(0,0, s.width, s.height)];
planet.center = o;
planet.backgroundColor = [UIColor orangeColor];
planet.layer.cornerRadius = s.width/2.0;
[self.view addSubview:planet];
float dAngle = M_PI / 3.0;
float r = 35;
for (int i=0; i<6; i++) {
float x = r * cos(dAngle * i) + o.x;
float y = r * sin(dAngle * i) + o.y;
UIView *satellite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
satellite.tag = i + 1;
satellite.center = CGPointMake(x, y);
satellite.backgroundColor = [UIColor lightGrayColor];
satellite.layer.cornerRadius = s.width/2.0;
[self.view addSubview:satellite];
}
}
– (void)copyScreen
{
CGSize imageSize = CGSizeMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds));
UIGraphicsBeginImageContext(imageSize);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *iv = [[UIImageView alloc] initWithImage:screenShot];
iv.center = CGPointMake(3.0 * CGRectGetMaxX(self.view.bounds)/4.0, CGRectGetMidY(self.view.bounds));
[self.view addSubview:iv];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj tag] > 0) {
SatelliteItem *satelliteItem = [[SatelliteItem alloc] initWithTarget:obj];
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:satelliteItem attachedToAnchor:satelliteItem.center];
[attachmentBehavior setFrequency:0.1];
[attachmentBehavior setDamping:2.0];
[animator addBehavior:attachmentBehavior];
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[satelliteItem] mode:UIPushBehaviorModeInstantaneous];
pushBehavior.angle = M_PI_4;
pushBehavior.magnitude = 0.1;
[animator addBehavior:pushBehavior];
[pushBehavior setActive:TRUE];
}
}];
self.animator = animator;
}
– (void)createLabel
{
UILabel *title = [[UILabel alloc] init];
NSString *strA = @”ebbinghaus”;
NSString *strB = @”illusion”;
NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@”%@ %@”,strA, strB]];
[atts addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:[atts.string rangeOfString:strA]];
[atts addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[atts.string rangeOfString:strB]];
[atts addAttribute:NSFontAttributeName value:[[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] fontWithSize:30] range:NSMakeRange(0, atts.length)];
title.attributedText = atts;
[title sizeToFit];
title.center = CGPointMake(CGRectGetMidX(self.view.bounds), 50);
[self.view addSubview:title];
}
@end