文档章节

UI代码练习-View的基本属性和自定义

TomatosX
 TomatosX
发布于 2015/01/07 11:33
字数 993
阅读 18
收藏 0
//
//  AppDelegate.m
//  View的基本属性和自定义
//
//  Created by on 14-12-19.
//  Copyright (c) 2014年 apple. All rights reserved.
//

// 用tag选取视图

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 100, 200, 100)];
//    view1.backgroundColor = [UIColor redColor];
    
//    当用colorWithRed来设置透明度的时候就不会存在挟持情况
//    因为你设置的是view1的背景颜色的透明度
//    而view1.alpha是对整个视图的透明度设置
//    这里的三基色值为0到1.0,如果美工给的是75,75,75的话,就如下书写
//    view1.backgroundColor = [UIColor colorWithRed:75/255.0 green:75/255.0 blue:75/255.0 alpha:0.5];
    view1.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    view1.tag = 1;
    
//    alpha是设置视图的透明度,默认为1.0,即不透明
//    使用alpha的时候需要注意:
//        他会挟持子视图,即当view1设置了透明度之后,其子视图也跟着设置了透明度
//    view1.alpha = 0.5;
    [self.window addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    view2.tag = 2;
    
//    设置view2隐藏,其子视图也跟着隐藏,这个属性也有挟持
//    view2.hidden = YES;
    [view1 addSubview:view2];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(50,50, 50, 50)];
    view3.backgroundColor = [UIColor blueColor];
    view3.tag = 3;
    [view2 addSubview:view3];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(90, 400, 140, 35);
    [button1 setTitle:@"改变颜色" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button1];
    
    return YES;
}

- (void)changeColor {
    UIView *view = [self.window viewWithTag:1];
    view.backgroundColor = [UIColor greenColor];
}

- (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:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "apple.View_________" in the application's documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"View_________" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    // Create the coordinator and store
    
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"View_________.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return _persistentStoreCoordinator;
}


- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

@end



© 著作权归作者所有

TomatosX
粉丝 23
博文 207
码字总数 119547
作品 0
成都
iOS工程师
私信 提问
HenCoder UI 部分 2-2 全新定义 View 的尺寸

这期是 HenCoder 布局部分的第二期:重写 onMeasure() 来全新定制自定义 View 的尺寸。 简介 这期虽然距离上期的时间比较久,但主要是我的个人原因,而不是因为这期的内容难。这期的内容还是...

2017/12/28
0
0
iOS 执行.py脚本生成UI层结构

上篇我们实现了架构代码生成器, 顿时觉得代码生成器这个东西只要掌握了写代码真的就是配置的问题了, 今天我们就来实现我一直以来就想做的功能 -- UI界面适配代码生成器, 和之前一样我们通过J...

Castie1
2017/11/25
0
0
HenCoder Android UI 部分 2-2 全新定义 View 的尺寸

这期是 HenCoder 布局部分的第二期:重写 onMeasure() 来全新定制自定义 View 的尺寸。 简介 这期虽然距离上期的时间比较久,但主要是我的个人原因,而不是因为这期的内容难。这期的内容还是...

扔物线
2017/12/28
0
0
HenCoder UI 部分 2-1 布局基础

HenCoder 的第二季:自定义 View 第二部分——布局过程的自定义,从这期正式开始了。好像有点小激动。 废话少说,直入正题。 简介 之前我说过,自定义 View 最关键的有三个点:绘制、布局和触...

扔物线
2017/11/06
0
0
Flutter 知识梳理 (自定义组件) - CustomPaint

一、背景知识 在中,当我们需要自定义的时候,需要重写方法,并通过结合的实现绘制的逻辑,的核心实现跟这个很类似。 在自定义组件中,有两个重要的概念: :它是的子类,我们将它放在树的节...

泽毛
07/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

golang-字符串-地址分析

demo package mainimport "fmt"func main() {str := "map.baidu.com"fmt.Println(&str, str)str = str[0:5]fmt.Println(&str, str)str = "abc"fmt.Println(&s......

李琼涛
今天
4
0
Spring Boot WebFlux 增删改查完整实战 demo

03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello 。这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD WebFlux 应用,让开发更方便。这里...

泥瓦匠BYSocket
今天
6
0
从0开始学FreeRTOS-(列表与列表项)-3

FreeRTOS列表&列表项的源码解读 第一次看列表与列表项的时候,感觉很像是链表,虽然我自己的链表也不太会,但是就是感觉很像。 在FreeRTOS中,列表与列表项使用得非常多,是FreeRTOS的一个数...

杰杰1号
今天
8
0
Java反射

Java 反射 反射是框架设计的灵魂(使用的前提条件:必须先得到代表的字节码的 Class,Class 类 用于表示.class 文件(字节码)) 一、反射的概述 定义:JAVA 反射机制是在运行状态中,对于任...

zzz1122334
今天
5
0
聊聊nacos的LocalConfigInfoProcessor

序 本文主要研究一下nacos的LocalConfigInfoProcessor LocalConfigInfoProcessor nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java p......

go4it
昨天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部