您的位置:首页 > 移动开发 > Android开发

android 和iOS的区别和联系《个人记录》

2016-10-08 22:54 190 查看
0: iOS的window 就相当于是android 中的Framelayout是一层一层的叠加上去的

1:iOS 中的视图可以直接添加子视图,

这个和android有很大的区别,android只能是viewgroup去添加子诗图,ios是view就可以

2:setRootViewController:[[MainUIViewController alloc] init]];//这个有点像java的setcontentview
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//首先改为村代码的方式,并且
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[MainUIViewController alloc] init]];//这个有点像java的setcontentview

return YES;
}


3: 子视图的添加有点像android 中的相对布局

//另外一个插入的方法,有三种形式,这种方式有点像android 中的相对位置
UIView *childView4 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[childView4 setBackgroundColor:[UIColor yellowColor]];
[childView insertSubview:childView4 atIndex:1];//在第1子视图的位置插入
//为了防止越界,可以
[childView insertSubview:childView4 atIndex:childView.subviews.count];
[childView insertSubview:childView4 aboveSubview:childView1];//在某个视图之上显示
[childView insertSubview:childView4 belowSubview:childView1];//在某个视图之下显示

4: 连续的图片的播放,ios直接通过图片的属性进行,而andoirdyou一个专门的animation去做

//
//  MainUIViewController.m
//  SevenUIImageView
//
//  Created by 千雅爸爸 on 16/10/10.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "MainUIViewController.h"

@interface MainUIViewController ()

@end

@implementation MainUIViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self.view setBackgroundColor:[UIColor greenColor]];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
[imageView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:imageView];

#if 0
//设置图片有两种方式,
//第一种方式:通过setimage的方法,注意了这个方式不适合很多很多图片的情况,

[imageView setImage:[UIImage imageNamed:@"cat"]];
//一定要注意了如果图片是png 的直接写名字就好了,如果是jpg的格式的需要全名
//http://easyicon.net/1141865-client_cat_icon.html

//第二种方式 通过nsdata,这个方式适合很多很多图片的情况
NSString *path = [[NSBundle mainBundle]pathForResource:@"a" ofType:@"ico"];//获取我们的项目的目录里面的图片的地址,注意了目录,如果我们的图片是和MainUIViewController是一个目录的,那么可以直接写,如果不是,例如在Assets.xcassets里面的最好就是使用第一种方式

NSData *imageData = [NSData dataWithContentsOfFile:path];
//最关键的如何把nsdata的对象转化为image的对象
UIImage *image = [UIImage imageWithData:imageData];
//通过这种方式加在的图片的缓存是不会缓存到内存中的,
[imageView setImage:image];

//图片视图的填充样式
imageView.contentMode = UIViewContentModeScaleToFill;//这个就是默认的
//UIViewContentModeScaleAspectFit//当一边到了零界点的时候就不拉升了
//UIViewContentModeScaleAspectFill//把最大的作为拉升依据,aspect是指宽高比不变

#endif
//放大招了,通过图片实现连续播放:图片下载,不同的尺寸http://easyicon.net/520367-christmas_ball_gold_icon.html
//这个和我们的android 中的动画有点像,但是这个好简单啊。
NSMutableArray<UIImage *> *images = [NSMutableArray array];
[images addObject:[UIImage imageNamed:@"128"]];
[images addObject:[UIImage imageNamed:@"96"]];
[images addObject:[UIImage imageNamed:@"72"]];
[images addObject:[UIImage imageNamed:@"64"]];

[imageView setAnimationImages:images];
[imageView setAnimationDuration:1.2];//设置间隔,也可以设置为0.2
[imageView setAnimationRepeatCount:0];//设置重播的次数,
[imageView startAnimating];//开始动画
[imageView setContentMode:UIViewContentModeCenter];//

// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#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


5: iOS 里面的TableView 其实就是android 里面的listview

//
//  MainViewController.m
//  TenNavigationViewTableView
//
//  Created by 千雅爸爸 on 16/10/16.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "MainViewController.h"
//TODO 切记切记,这里一定要设置实现协议,不然scrollViewDidScroll就不能够找到
@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;//TableView其实就是android 中的listview
@property (nonatomic,strong) UINavigationBar *navigationBar;

@end

@implementation MainViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor greenColor]];
//隐藏系统自带的导航条,因为我们要实现渐变的效果,导航栏是自定义的
[self.navigationController setNavigationBarHidden:YES];
//让导航栏不影响tableview的视图,这样添加的起始坐标,
[self setAutomaticallyAdjustsScrollViewInsets:NO];
//这样添加的tableview就不会做自动挡饿调整了
[self intiWithNavigationBar];

[self initTableView];

// Do any additional setup after loading the view.
}

-(void) initTableView{
[self setTableView:[[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain]];//CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)获取空间的长度,有点像android 里面的获取空间长度的方法
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];//有点像android 里面的padding上坐下右;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];

}

-(void)intiWithNavigationBar{
[self setNavigationBar:[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)]];
[self.navigationBar setBackgroundColor:[UIColor yellowColor]];//
//将不透明的效果取消掉
[self.navigationBar setTranslucent:NO];
[self.navigationController.view addSubview:self.navigationBar];

//如果背景层不移除的话,会有问题
for (UIView *subView in self.navigationBar.subviews) {
if([subView isKindOfClass:[NSClassFromString(@"_UINavigationBarBackground") class]]){
[subView removeFromSuperview];
}
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate -
//这里相当于java中的实现了接口后的方法,
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
UIColor *color = self.navigationBar.backgroundColor;
CGFloat offsetY = scrollView.contentOffset.y;//往上为正
if(offsetY>0){
CGFloat alpha  = (600-offsetY)/600;
self.navigationBar.backgroundColor =[color colorWithAlphaComponent:alpha];
}else{
self.navigationBar.backgroundColor =[color colorWithAlphaComponent:1];
}
}

#pragma mark - Table view data source -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];//会从重用队列里面去取
[cell.textLabel setText:[NSString stringWithFormat:@"Row-%zd",indexPath.row]];
[cell setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:1.0/(arc4random()%5)] ];//产生随机数的方法,arc4random(),和java略有不同

return cell;
}

/*
#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


6: 随机数的产生的方法

[cell setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:1.0/(arc4random()%5)] ];//产生随机数的方法,arc4random(),和java略有不同


7:ios里面的协议,就是android 中的接口,复习一下

//TODO 切记切记,这里一定要设置实现协议,不然scrollViewDidScroll就不能够找到
@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>


8: android 里面的点击之后的切换的selector 和iOS 里面的selected比较像

FirstViewController *firstVC=[[FirstViewController alloc]init];
//把first先放在navigationcontroler上面,然后再将navigationcontroller添加到
firstVC.tabBarItem.title = @"first";//地下的说明的文字
firstVC.tabBarItem.image = [UIImage imageNamed:@"64"];//默认的图片
//firstVC.tabBarItem.selectedImage =[UIImage imageNamed:@"cat"];//选中后的效果
//默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接
//这里的选中和补选中的显示就和android 中的selector很像
firstVC.tabBarItem.selectedImage =[UIImage originalImageName:@"cat"];//选中后的效果
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstVC];


9: ios导航栏控制器和android 里面的viewpager和radiogroup的结合很像

//
// AppDelegate.m
// EllevenBiaoqianLan
//
// Created by 千雅爸爸 on 16/10/27.
// Copyright © 2016年 kodulf. All rights reserved.
//

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
//默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接
//导入我们创建的分类
#import "UIImage+OriginalImage.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//导航栏控制器是一种栈的形式,
//标签栏控制器,就有点像是android 里面的ViewPager和RadioGroup结合的形式,但是又是不同的,标签栏控制器里面的组件,例如有导航栏控制器,有其它的,会都显示只不过是有的在前面,有的在后面,相当于是并列的显示,请查看说明的图
//我们一板是将导航栏控制器设置为标签栏控制器的子控制器
//首先创建四个控制器的类,然后是一个UIImage+OriginalImage的分类

[self setWindow:[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]];
[self.window makeKeyAndVisible];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController.view setBackgroundColor:[UIColor whiteColor]
];
[self.window setRootViewController:tabBarController];
FirstViewController *firstVC=[[FirstViewController alloc]init]; //把first先放在navigationcontroler上面,然后再将navigationcontroller添加到 firstVC.tabBarItem.title = @"first";//地下的说明的文字 firstVC.tabBarItem.image = [UIImage imageNamed:@"64"];//默认的图片 //firstVC.tabBarItem.selectedImage =[UIImage imageNamed:@"cat"];//选中后的效果 //默认上面的图片都是做过了毛玻璃的效果的,如果不想要毛玻璃的效果,那么久直接 //这里的选中和补选中的显示就和android 中的selector很像 firstVC.tabBarItem.selectedImage =[UIImage originalImageName:@"cat"];//选中后的效果 UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:firstVC];//直接复制粘贴上面的first 生成second third
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.tabBarItem.title =@"second";
secondVC.tabBarItem.image = [UIImage imageNamed:@"64"];
secondVC.tabBarItem.selectedImage=[UIImage originalImageName:@"cat"];
UINavigationController *secondNavigationController = [[UINavigationController alloc]initWithRootViewController:secondVC];

ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
thirdVC.tabBarItem.title = @"third";
thirdVC.tabBarItem.image =[UIImage imageNamed:@"64"];
thirdVC.tabBarItem.selectedImage = [UIImage originalImageName:@"cat"];
UINavigationController *thirdNavigationController = [[UINavigationController alloc]initWithRootViewController:thirdVC];

//还可以给tabbar 设置背景图片和颜色
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"green2"]];
//[tabBarController.tabBar setBackgroundColor:[UIColor greenColor]];

//统一设置字体的大小
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
[tabBarController setViewControllers:@[navigationController,secondNavigationController,thirdNavigationController]];
//默认的tabbar选择的是第一个被加入到数组中的
//懒加载的方式,用到哪个就去加在哪个,而且一旦加载了不会轻易的被回收
//但是如果出现内存警告了,那么系统会优先处理掉那些当前用不到的界面

// Override point for customization after application launch.
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


10: 有一点和android 比较像的是设置主视图,我们可以在我们的项目点击,然后是Main
Interface 里面选择

和android 里面的清单文件中的launch一样的属性

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: