iOS_2_按鈕控制物體形變,ios_2按鈕物體形變
最終:
BeyondViewController.h
//// BeyondViewController.h// 02_按鈕控制物體形變//// Created by beyond on 14-7-21.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>@interface BeyondViewController : UIViewController// 控制器成員記住介面上的頭像按鈕@property (weak, nonatomic) IBOutlet UIButton *headBtn;// 按鈕控制 head button 上 下 左 右 移動- (IBAction)btnClick:(UIButton *)sender;- (IBAction)AffineTransform:(UIButton *)sender;- (IBAction)reset:(UIButton *)sender;@end
BeyondViewController.m
//// BeyondViewController.m// 02_按鈕控制物體形變//// Created by beyond on 14-7-21.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"#define kDelta 20const int DELTA = 50;@interface BeyondViewController (){ // 左旋轉 最笨方法 成員變數 記住弧度 可累計 CGFloat _angel; // 成員記住 headBtn預設的frame CGRect _headBtnFrame;}@end@implementation BeyondViewController- (void)viewDidLoad{ [super viewDidLoad]; // view一載入 就用 成員 記住 headBtn的初始位置 _headBtnFrame = _headBtn.frame; // 調用自訂方法,代碼建立 buttuon [self addButtionByCoding]; [self addTextFieldByCoding];}# pragma mark - 按鈕控制 head button 上 下 左 右 移動- (void)moveByFrame:(UIButton *)sender{ // UIView的類方法 實現動畫效果(開始動畫) [UIView beginAnimations:nil context:nil]; // 預設動畫期間是 0.2 [UIView setAnimationDuration:1]; // 以下三步為OC標準代碼,因為OC中不允許直接修該對象中結構體屬性的成員的值,要通過中間的臨時結構體變數 CGRect frame = self.headBtn.frame; // 一般數字是一樣的話就可以抽取為: 1,變數; 2,宏; 3,const int // CGFloat delta = 50; // #define kDelta 50 // const int DELTA = 50; int tag = [sender tag]; switch (tag) { case 1: frame.origin.y -= kDelta; break; case 2: frame.origin.x += kDelta; break; case 3: frame.origin.y += kDelta; break; case 4: frame.origin.x -= kDelta; break; default: break; } self.headBtn.frame=frame; // UIView的類方法 實現動畫效果(結束動畫) [UIView commitAnimations];}- (IBAction)btnClick:(UIButton *)sender { [self animateWithBlock:^{ // 以下三步為OC標準代碼,因為OC中不允許直接修該對象中結構體屬性的成員的值,要通過中間的臨時結構體變數 CGPoint center = self.headBtn.center; // 一般數字是一樣的話就可以抽取為: 1,變數; 2,宏; 3,const int // CGFloat delta = 50; // #define kDelta 50 // const int DELTA = 50; int tag = [sender tag]; switch (tag) { case 1: center.y -= kDelta; break; case 2: center.x += kDelta; break; case 3: center.y += kDelta; break; case 4: center.x -= kDelta; break; default: break; } self.headBtn.center = center; }]; }#pragma mark - 按鈕控制 head button 左右旋轉 放大 縮小- (IBAction)AffineTransform:(UIButton *)sender { // UIView的類方法 實現動畫效果(開始動畫) [UIView beginAnimations:nil context:nil]; // 預設動畫期間是 0.2 [UIView setAnimationDuration:1]; int tag = [sender tag]; switch (tag) { case 11: // _angel -= M_PI_4; // 旋轉 順時針為正方向,使用弧度 M_PI_4 就是順時針旋轉45度 //_headBtn.transform = CGAffineTransformMakeRotation(_angel); // CGAffineTransformRotate方法:返回一個新的結構體,是一個在原來 的結構體基礎上進行一定弧度旋轉的新的結構體 _headBtn.transform = CGAffineTransformRotate(_headBtn.transform, - M_PI_4); break; case 12: // _angel += M_PI_4; // 旋轉 順時針為正方向,使用弧度 M_PI_4 就是順時針旋轉45度 //_headBtn.transform = CGAffineTransformMakeRotation(_angel); // CGAffineTransformRotate方法:返回一個新的結構體,是一個在原來 的結構體基礎上進行一定弧度旋轉的新的結構體 _headBtn.transform = CGAffineTransformRotate(_headBtn.transform, M_PI_4); break; case 13: // 縮小 // _headBtn.transform = CGAffineTransformMakeScale(0.5, 0.5); _headBtn.transform = CGAffineTransformScale(_headBtn.transform, 0.8, 0.8); break; case 14: // 放大 // _headBtn.transform = CGAffineTransformMakeScale(1.5, 1.5); _headBtn.transform = CGAffineTransformScale(_headBtn.transform, 1.2, 1.2); break; case 0: // 點擊headBtn的時候,清空並還原為預設狀態 _headBtn.transform = CGAffineTransformIdentity; _headBtn.frame = _headBtnFrame; break; default: break; } // UIView的類方法 實現動畫效果(結束動畫) [UIView commitAnimations];}#pragma mark - 通過block封裝代碼// void (^myBlock)();void (^myBlock)() = ^{ NSLog(@"beyond");};// 手動調用block()時有點問題- (void)animateWithBlock:(void(^)())block{ // UIView的類方法 實現動畫效果(開始動畫) [UIView beginAnimations:nil context:nil]; // 預設動畫期間是 0.2 [UIView setAnimationDuration:1]; block(); // UIView的類方法 實現動畫效果(結束動畫) [UIView commitAnimations];}- (IBAction)reset:(UIButton *)sender { [self animateWithBlock:^{ // 點擊的時候,清空並還原為預設狀態 _headBtn.transform = CGAffineTransformIdentity; _headBtn.frame = _headBtnFrame; }];}- (void) addButtionByCoding{ // 1,用類方法建立 button執行個體 UIButton *button = [[UIButton alloc] init]; // 2,設定button的細節 button.frame = CGRectMake(0, 0, 100, 100); // 正常狀態 [button setTitle:@"normal" forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // [button setImage:[UIImage imageNamed:@"btn_01.png"] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"btn_01.png"] forState:UIControlStateNormal]; // 點擊時高亮狀態 [button setTitle:@"highlighted" forState:UIControlStateHighlighted]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; // [button setImage:[UIImage imageNamed:@"btn_02.png"] forState:UIControlStateHighlighted]; [button setBackgroundImage:[UIImage imageNamed:@"btn_02.png"] forState:UIControlStateHighlighted]; // 為按鈕添加點擊事件 [button addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside]; // 3,添加button到當前控制器的view裡面 [self.view addSubview:button];}// 代碼建立的按鈕的點擊事件- (void) codeBtnClick:(UIButton *)sender{ NSLog(@"%@",sender); NSLog(@"%p",sender);}// 代碼建立文本輸入框- (void) addTextFieldByCoding{ // 1,類方法建立控制項 UITextField *textField = [[UITextField alloc]init]; // 2,控制項細節 textField.frame = CGRectMake(100, 0, 100, 100); textField.backgroundColor = [UIColor grayColor]; // 系統字型大小 textField.font = [UIFont systemFontOfSize:20]; textField.font = [UIFont boldSystemFontOfSize:30]; // 置中顯示 CGFloat x = self.view.frame.size.width*0.5; CGFloat y = self.view.frame.size.height*0.5; // textField.center = CGPointMake(x, y); // 以下三步為OC標準代碼,因為OC中不允許直接修該對象中結構體屬性的成員的值,要通過中間的臨時結構體變數 CGPoint center = textField.center; center.x = x; center.y = y; textField.center = center; // 3,將控制項添加到當前控制器的view [self.view addSubview:textField];}@end
力不一定使物體發生形變 對
不對 力作用在任何物體上都會產生形變
只是有時肉眼看不出 分子間的變化
事實上只要有力作用在物體上都會發生形變
而有時會改變物體運動狀態
如果兩個物體間有彈力的作用,那這兩個物體一定發生形變
一定
你看看彈力定義
【比如被拉長的彈簧.】