Skip to content

Commit

Permalink
完了 ひと通りの動作を確認
Browse files Browse the repository at this point in the history
  • Loading branch information
chohey committed Oct 27, 2013
1 parent e33acb5 commit e71e6a8
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 104 deletions.
2 changes: 1 addition & 1 deletion IKCustomMenuView/IKCustomMenuView/IKViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <UIKit/UIKit.h>

@interface IKViewController : UIViewController
@interface IKViewController : UIViewController <UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *backImageView;
@property (strong, nonatomic) IBOutlet UIButton *btn1;
Expand Down
192 changes: 190 additions & 2 deletions IKCustomMenuView/IKCustomMenuView/IKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface IKViewController ()
@property (nonatomic,strong) NSMutableString *caseString;
@property int currentCameraX, currentCameraY, currentDataX, currentDataY, reverse;
@property int currentBtn1X, currentBtn1Y, currentBtn2X, currentBtn2Y, reverse;
@property (nonatomic, strong) UILabel *naviTitleLabel;
@property (strong, nonatomic) NSTimer *shakeTimer;
@end
Expand All @@ -20,7 +20,55 @@ @implementation IKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self longPressSetup];
[self uiSetup];
}
- (void)longPressSetup{
// ボタンの長押し検出設定
UILongPressGestureRecognizer *gestureRecognizerData = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedHandler:)];
[self.btn2 addGestureRecognizer:gestureRecognizerData];
UILongPressGestureRecognizer *gestureRecognizerCamera = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedHandler:)];
[self.btn1 addGestureRecognizer:gestureRecognizerCamera];
}
- (void)uiSetup{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud objectForKey:@"backImage"]) {
[self.backImageView setImage:[[UIImage alloc] initWithData:[ud objectForKey:@"backImage"]]];
}else{
[self.backImageView setImage:[UIImage imageNamed:@"backImage.png"]];
}
if ([ud objectForKey:@"buttonImage"]) {
[self.btn1 setBackgroundImage:[[UIImage alloc] initWithData:[ud objectForKey:@"buttonImage"]] forState:UIControlStateNormal];
[self.btn2 setBackgroundImage:[[UIImage alloc] initWithData:[ud objectForKey:@"buttonImage"]] forState:UIControlStateNormal];
}else{
[self.btn1 setBackgroundImage:[UIImage imageNamed:@"baseball.png"] forState:UIControlStateNormal];
[self.btn2 setBackgroundImage:[UIImage imageNamed:@"baseball.png"] forState:UIControlStateNormal];
}
if ([ud objectForKey:@"btnPoint"]) {
NSDictionary *dict = [ud objectForKey:@"btnPoint"];
self.btn1.center = CGPointMake([[dict objectForKey:@"btn1X"] integerValue], [[dict objectForKey:@"btn1Y"] integerValue]);
self.btn2.center = CGPointMake([[dict objectForKey:@"btn2X"] integerValue], [[dict objectForKey:@"btn2Y"] integerValue]);
}else{
self.btn1.center = CGPointMake(160,124);
self.btn2.center = CGPointMake(160,240);
}
UIColor *color = [UIColor blackColor];
UIColor *alphaColor = [color colorWithAlphaComponent:0.0];
self.coverView.backgroundColor = alphaColor;
self.currentBtn1X = self.btn1.frame.origin.x+self.btn1.frame.size.width/2;
self.currentBtn1Y = self.btn1.frame.origin.y+self.btn1.frame.size.height/2;
self.currentBtn2X = self.btn2.frame.origin.x+self.btn2.frame.size.width/2;
self.currentBtn2Y = self.btn2.frame.origin.y+self.btn2.frame.size.height/2;
}
- (void)viewWillAppear:(BOOL)animated
{
self.naviTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.naviTitleLabel.font = [UIFont boldSystemFontOfSize:17.0];
self.naviTitleLabel.textColor = [UIColor colorWithRed:0.8f green:0.2f blue:0.2f alpha:1.0];
self.naviTitleLabel.backgroundColor = [UIColor clearColor];
self.navigationItem.titleView = self.naviTitleLabel;
self.naviTitleLabel.text = @"        ";
[self.naviTitleLabel sizeToFit];
}

- (void)didReceiveMemoryWarning
Expand All @@ -30,5 +78,145 @@ - (void)didReceiveMemoryWarning
}

- (IBAction)pushCustomBtn:(id)sender {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"レイアウトの変更" message:nil delegate:self cancelButtonTitle:@"背景画像変更" otherButtonTitles:@"ボタン画像変更",@"ボタン位置変更",@"初期化",@"キャンセル",nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:{
// 背景画像変更
self.caseString = [NSMutableString stringWithString:@"backImage"];
[self imagePickerSetup];
break;
}
case 1:{
// ボタン画像変更
self.caseString = [NSMutableString stringWithString:@"buttonImage"];
[self imagePickerSetup];
break;
}
case 2:{
// ボタン位置変更
[self.view bringSubviewToFront:self.coverView];
self.naviTitleLabel.text = @"ボタン移動モード";
self.shakeTimer = [NSTimer scheduledTimerWithTimeInterval:0.14f target:self selector:@selector(shake) userInfo:nil repeats:YES];
self.reverse = 1;
[self.shakeTimer fire];
break;
}
case 3:{
// 初期化
[self.backImageView setImage:[UIImage imageNamed:@"backImage.png"]];
[self.btn1 setBackgroundImage:[UIImage imageNamed:@"baseball.png"] forState:UIControlStateNormal];
[self.btn2 setBackgroundImage:[UIImage imageNamed:@"baseball.png"] forState:UIControlStateNormal];
self.btn1.center = CGPointMake(160,124);
self.btn2.center = CGPointMake(160,240);
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud removeObjectForKey:@"backImage"];
[ud removeObjectForKey:@"buttonImage"];
[ud removeObjectForKey:@"btnPoint"];
[ud synchronize]; // 一応すぐに反映させる
break;
}
case 4:
// cancelButtonが押されたときのアクション
break;
default:
break;
}
}
- (void)imagePickerSetup{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; // 生成
ipc.delegate = self; // デリゲートを自分自身に設定
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 画像の取得先をカメラに設定
ipc.allowsEditing = YES; // 画像取得後編集する
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// UIImagePNGRepresentation関数によりUIImageが保持する画像データをPNG形式で抽出
NSData* pngData = [[NSData alloc] initWithData:UIImagePNGRepresentation( image )];
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([self.caseString isEqualToString:@"backImage"]) {
[self.backImageView setImage:image];
[ud setObject:pngData forKey:@"backImage"];
}else{
[self.btn1 setBackgroundImage:image forState:UIControlStateNormal];
[self.btn2 setBackgroundImage:image forState:UIControlStateNormal];
[ud setObject:pngData forKey:@"buttonImage"];
}
[self dismissViewControllerAnimated:NO completion:nil];
}

-(void)longPressedHandler:(UILongPressGestureRecognizer *)gestureRecognizer{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan://長押しを検知開始
{
NSLog(@"UIGestureRecognizerStateBegan");
[self.view bringSubviewToFront:self.coverView];
self.naviTitleLabel.text = @"ボタン移動モード";
self.shakeTimer = [NSTimer scheduledTimerWithTimeInterval:0.14f target:self selector:@selector(shake) userInfo:nil repeats:YES];
self.reverse = 1;
[self.shakeTimer fire];
}
break;
default:
break;
}
}
-(void)shake{
[UIView animateWithDuration:0.07f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
// 揺らす
self.btn1.transform = CGAffineTransformMakeRotation(self.reverse * M_PI * 3 / 180.0);
self.btn2.transform = CGAffineTransformMakeRotation(self.reverse * M_PI * 3 / 180.0);
self.reverse *= -1;
} completion:^(BOOL finished) {
// アニメーションが終わった後実行する処理
}];
}

// 画面に指を一本以上タッチしたときに実行されるメソッド
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// タッチ点がボタン上でなければ移動モード解除 ポイントを記録する
CGPoint p = [[touches anyObject] locationInView:self.view];
if (![self checkInBtn:self.btn1 point:p] && ![self checkInBtn:self.btn2 point:p]) {
[self.view sendSubviewToBack:self.coverView];
self.naviTitleLabel.text = @"        ";
NSDictionary *pointDict = [[NSDictionary alloc] initWithObjects:@[[NSNumber numberWithInt:self.currentBtn1X], [NSNumber numberWithInt:self.currentBtn1Y], [NSNumber numberWithInt:self.currentBtn2X], [NSNumber numberWithInt:self.currentBtn2Y]] forKeys:@[@"btn1X",@"btn1Y",@"btn2X",@"btn2Y"]];
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:pointDict forKey:@"btnPoint"];
NSLog(@"ポイント保存!!:%@",pointDict);
[self.shakeTimer invalidate];
self.btn1.transform = CGAffineTransformMakeRotation(0.0 / 180.0);
self.btn2.transform = CGAffineTransformMakeRotation(0.0 / 180.0);
}
}
// 画面に触れている指が一本以上移動したときに実行されるメソッド
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// ボタン上であればボタンを移動させる
CGPoint p = [[touches anyObject] locationInView:self.view];
if ([self checkInBtn:self.btn1 point:p]) {
self.currentBtn1X = p.x;
self.currentBtn1Y = p.y;
self.btn1.center = CGPointMake(self.currentBtn1X, self.currentBtn1Y);
}
if ([self checkInBtn:self.btn2 point:p]) {
self.currentBtn2X = p.x;
self.currentBtn2Y = p.y;
self.btn2.center = CGPointMake(self.currentBtn2X, self.currentBtn2Y);
}
}
- (BOOL)checkInBtn:(UIButton *)btn point:(CGPoint)point
{
CGPoint b = btn.frame.origin;
return (((b.x < point.x) && ((b.x+btn.frame.size.width) > point.x)) && ((b.y < point.y) && ((b.y+btn.frame.size.height) > point.y)));
}
@end
Loading

0 comments on commit e71e6a8

Please sign in to comment.