你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI04_Touch

iOS UI04_Touch

編輯:IOS開發綜合

1.實現的是微信的搖一搖
2.實現的是View隨著觸摸能夠在視圖中進行移動,還有是通過觸碰實現顏色變換,一碰顏色就會得到改變

//
//  AppDelegate.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import AppDelegate.h
#import MainViewController.h
@interface AppDelegate ()

@end

@implementation AppDelegate
-(void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    //設置根視圖控制器
    MainViewController *mainVC =[[MainViewController alloc] init];
    self.window.rootViewController=mainVC;
    [mainVC release];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
//
//  MyView.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import MyView.h
//補上延展
@interface MyView ()
//用來記錄視圖開始的坐標
@property(nonatomic,assign)CGPoint startPoint;

@end

@implementation MyView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    //集合中元素個數
    NSLog(@%ld,touches.count);
    //集合裡有一個觸摸類的對象
    UITouch *touch=[touches anyObject];
    //通過觸摸對象獲取相應視圖的當前位置
    self.startPoint=[touch locationInView:self];
    NSLog(@%g,self.startPoint.x);
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //通過移動,找到變化,然後讓MyView也進行相應的調整,從而實現視圖隨手移動的效果
    //獲取觸摸對象
    UITouch *touch=[touches anyObject];
    //獲取移動之後坐標
    CGPoint movePoint=[touch locationInView:self];
    //找坐標的變換
    CGFloat dx=movePoint.x - self.startPoint.x;
    CGFloat dy=movePoint.y - self.startPoint.y;
    //設置視圖的移動變化
    self.center=CGPointMake(self.center.x + dx, self.center.y + dy);

}
@end
//
//  MainViewController.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 cml. All rights reserved.
//

#import MainViewController.h
#import MyView.h

@interface MainViewController ()
// textField
@property(nonatomic, retain)UITextField *myTextField;

@end

@implementation MainViewController

-(void)dealloc{
    [_myTextField release];
    [super dealloc];
}



//-(void)loadView
//{
//    [super loadView];
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // ViewController中初始化方法,loadView,viewDidLoad只會運行一次,但是viewappear只要視圖顯示,就會運行一次

    self.myTextField =[[UITextField alloc] initWithFrame:CGRectMake(100, 500, 150, 40)];
    self.myTextField.layer.cornerRadius =10;
    self.myTextField.layer.borderWidth =2;
    [self.view addSubview:_myTextField];
    [_myTextField release];



//    // 創建一個MyView;
//    MyView *myView=[[MyView alloc] initWithFrame:CGRectMake(100, 200, 150, 40)];
//    myView.backgroundColor =[UIColor redColor];
//    [self.view addSubview:myView];
//    [myView release];

    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    view.backgroundColor =[UIColor yellowColor];
    [self.view addSubview:view];
    [view release];


    UIButton *button1=[UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame=CGRectMake(0, 0, 100, 100);
    button1.backgroundColor=[UIColor cyanColor];
    button1.layer.borderWidth=1;
    button1.layer.cornerRadius=10;
    [view addSubview:button1];
    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame=CGRectMake(150,150, 100, 100);
    button2.backgroundColor=[UIColor blackColor];
    button2.layer.borderWidth=1;
    button2.layer.cornerRadius=10;
    [view addSubview:button2];
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];



}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@觸摸開始);
    // 點擊空白處,回收鍵盤
    [self.myTextField resignFirstResponder];

}
-(void)click:(UIButton *)button
{
    NSLog(@測試是否執行);
    [self.myTextField becomeFirstResponder];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@觸摸結束);
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@觸摸被取消);
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@觸摸移動);
}

//hardview中 shake Gestrue 模擬搖一搖
// 搖一搖
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@搖一搖開始);
    self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

}


-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@搖一搖結束);
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@搖一搖被取消);
}


// hardview 中Simulate Memory Warning 模擬內存管理
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@內存警告);
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved