Skip to content

Instantly share code, notes, and snippets.

@20m61
Created December 3, 2012 05:29
Show Gist options
  • Save 20m61/4192958 to your computer and use it in GitHub Desktop.
Save 20m61/4192958 to your computer and use it in GitHub Desktop.
ボタンの配置+ボタン・背景の色の変化(ボタンの色変化は暫定)
//
// ViewController.h
// 121203
//
// Created by changhwi on 12/12/03.
// Copyright (c) 2012年 changhwi. All rights reserved.
//
#import <UIKit/UIKit.h>
//RGB,Alpha値
float valueR = 1.;
float valueG = 1.;
float valueB = 1.;
float valueAlpha = 0.3;
float chengeColor = 0.2;
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *viewFirst;
@end
//
// ViewController.m
// 121203
//
// Created by changhwi on 12/12/03.
// Copyright (c) 2012年 changhwi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self buttonCreat];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonCreat{
int center = self.view.frame.size.width / 2;
int bottom = self.view.frame.size.height;
int bottunWIdth = 60;
int bottunheight = 40;
int bottomMargin = 80;
int buttonMargine = 20;
int buttonTypeChenge = nil;
#pragma mark - buttonR
//https://gist.github.com/1f6932ec3b8407c4c409
UIButton *buttonRPlus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonRPlus setTitle:@"R+" forState:UIControlStateNormal];
buttonRPlus.frame =
CGRectMake(center - bottunWIdth / 2 - buttonMargine - bottunWIdth, bottom - bottomMargin - bottunheight - buttonMargine, bottunWIdth, bottunheight);
buttonRPlus.backgroundColor = [UIColor colorWithRed:1 - valueR green:0. blue:0. alpha:valueAlpha];
[self.view addSubview:buttonRPlus];
//ボタンアクションの設定
[buttonRPlus addTarget:self
action:@selector(buttonRPlusMethod:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonRMinus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonRMinus setTitle:@"G-" forState:UIControlStateNormal];
buttonRMinus.frame = CGRectMake(center - bottunWIdth / 2 - buttonMargine - bottunWIdth, bottom - bottomMargin, bottunWIdth, bottunheight);
buttonRMinus.backgroundColor = [UIColor colorWithRed:valueR green:0. blue:0. alpha:valueAlpha];
[self.view addSubview:buttonRMinus];
//ボタンアクションの設定
[buttonRMinus addTarget:self
action:@selector(buttonRMinusMethod:)
forControlEvents:UIControlEventTouchUpInside];
#pragma mark - buttonG
UIButton *buttonGPlus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonGPlus setTitle:@"G+" forState:UIControlStateNormal];
buttonGPlus.frame =
CGRectMake(center - bottunWIdth / 2, bottom - bottomMargin - bottunheight - buttonMargine, bottunWIdth, bottunheight);
buttonGPlus.backgroundColor = [UIColor colorWithRed:0. green:1 - valueG blue:0. alpha:valueAlpha];
[self.view addSubview:buttonGPlus];
//ボタンアクションの設定
[buttonGPlus addTarget:self
action:@selector(buttonGPlusMethod:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonGMinus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonGMinus setTitle:@"G-" forState:UIControlStateNormal];
buttonGMinus.frame = CGRectMake(center - bottunWIdth / 2, bottom - bottomMargin, bottunWIdth, bottunheight);
buttonGMinus.backgroundColor = [UIColor colorWithRed:0. green:valueG blue:0. alpha:valueAlpha];
[self.view addSubview:buttonGMinus];
//ボタンアクションの設定
[buttonGMinus addTarget:self
action:@selector(buttonGMinusMethod:)
forControlEvents:UIControlEventTouchUpInside];
#pragma mark - buttonB
UIButton *buttonBPlus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonBPlus setTitle:@"B+" forState:UIControlStateNormal];
buttonBPlus.frame =
CGRectMake(center - bottunWIdth / 2 + buttonMargine + bottunWIdth, bottom - bottomMargin - bottunheight - buttonMargine, bottunWIdth, bottunheight);
buttonBPlus.backgroundColor = [UIColor colorWithRed:0. green:0. blue:1 - valueB alpha:valueAlpha];
[self.view addSubview:buttonBPlus];
//ボタンアクションの設定
[buttonBPlus addTarget:self
action:@selector(buttonBPlusMethod:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonBMinus =
[UIButton buttonWithType:buttonTypeChenge];
[buttonBMinus setTitle:@"B-" forState:UIControlStateNormal];
buttonBMinus.frame = CGRectMake(center - bottunWIdth / 2 + buttonMargine + bottunWIdth, bottom - bottomMargin, bottunWIdth, bottunheight);
buttonBMinus.backgroundColor = [UIColor colorWithRed:0. green:0. blue:valueB alpha:valueAlpha];
[self.view addSubview:buttonBMinus];
//ボタンアクションの設定
[buttonBMinus addTarget:self
action:@selector(buttonBMinusMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - viewUpdate
- (void)viewUpdate{
[UIView animateWithDuration:0.1
animations:^{self.view.backgroundColor = [UIColor colorWithRed:valueR green:valueG blue:valueB alpha:valueAlpha ];}
];
}
#pragma mark - buttonRUpdateMethod
- (void)buttonRPlusMethod:(UIButton *)buttonRPlus{
if (valueR <= 1) {
valueR = valueR + chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonRPlus];
NSLog(@"%f", valueR);
}
- (void)buttonRMinusMethod:(UIButton *)buttonRMinus{
if (valueR >= 0) {
valueR = valueR - chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonRMinus];
NSLog(@"%f", valueR);
}
#pragma mark - buttonGUpdateMethod
- (void)buttonGPlusMethod:(UIButton *)buttonGPlus{
if (valueG <= 1) {
valueG = valueG + chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonGPlus];
NSLog(@"%f", valueG);
}
- (void)buttonGMinusMethod:(UIButton *)buttonGMinus{
if (valueG >= 0) {
valueG = valueG - chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonGMinus];
NSLog(@"%f", valueG);
}
#pragma mark - buttonBUpdateMethod
- (void)buttonBPlusMethod:(UIButton *)buttonBPlus{
if (valueB <= 1) {
valueB = valueB + chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonBPlus];
NSLog(@"%f", valueB);
}
- (void)buttonBMinusMethod:(UIButton *)buttonBMinus{
if (valueB >= 0) {
valueB = valueB - chengeColor;
}
[self viewUpdate];
[self buttonCreat];
[self.view addSubview:buttonBMinus];
NSLog(@"%f", valueB);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment