【www.bbyears.com--ios】
通过仿密码生成器软件,练习IOS开发技术,加深对MVC设计模式的理解,对以前学习的点点滴滴复习+掌握。因为看到的例子是用拖拉界面实现的,
而为了实现和更好地学习IOS开发,我采用纯编码的方式来开发,所以相对拖拉会比较慢。例子里面虽然有专门的布局方法,但是没有处理屏幕方向发生变化时的事件,所以横屏还是有问题的。此外,对于每个界面都有一个对应的控制类,在UIView类里面实现UI元素的添加布局,在控制器里面实现事件、逻辑的处理,以便符合MVC的设计模式。
结果展示
主要技术点
程序主要有两个界面:主页面(MainView)和帮助页面(InfomationView),这两个视图都是以程序的子视图的方式显示,两者互斥出现
在主页面显示的时候,程序的根视图有一个信息按钮,通过该按钮可导航到帮助页面,而在帮助页面时,程序的根视图顶部有一个导航栏,通过导航栏的左右按钮都可以返回到主页面。
页面切换时的动画采用UIView动画,其基本用法形如下所示:
代码如下 [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self removeMainView];
[UIView commitAnimations];
一个UINavigationItem分为三部分:左按钮,标题文字,右按钮,主要用法形如:
代码如下
UINavigationBar* bar = [[UINavigationBar alloc] init];
[self.view insertSubview:bar aboveSubview:self.infomationViewController.view];
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"About Password Generator"];
[bar pushNavigationItem:item animated:YES];
UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)];
UIBarButtonItem* rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)];
item.leftBarButtonItem = leftBarButton;
item.rightBarButtonItem = rightBarButton;
由于UIView的类里面初始化的时候只是添加了控件,而没有对控件的布局进行初始化,而布局初始化又是类方法里面的reLayout,所以为了保证显示前能够正确调用布局,我在视图对应类的控制器类里面重写了方法- (void) viewWillAppear:(BOOL)animated,在该方法里面调用了布局方法,如下所示:
代码如下 - (void) viewWillAppear:(BOOL)animated
{
if(self.autolayout)
{
[self.view reLayout];
}
[super viewWillAppear:animated];
}
由于经验不够,本来想想就能完成的事情做起来还是出现乱七八糟的问题,首先昨晚花了一个多小时一直写代码,然后测试,居然没有画面。调了下,不知道是什么原因。然后今天新建了个工程,一边写,一边看效果,零零散散,总算完成了。看来..不能一口吃成胖子,也不能光说不练。
主要代码
程序主控制器
代码如下
//
// ViewController.m
// PasswordGenerator
//
// Created by arbboter on 14/12/23.
// Copyright (c) 2014ๅนด arbboter. All rights reserved.
//
#import "ViewController.h"
#import "MainViewController.h"
#import "InfomationViewController.h"
@interface ViewController ()
@property(nonatomic, retain) UIButton* infomationButton;
@property(nonatomic, retain) MainViewController* mainViewController;
@property(nonatomic, retain) InfomationViewController* infomationViewController;
@property (nonatomic, retain) UINavigationBar* navagationBar;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self SwitchView];
}
- (void) onInfomationView
{
InfomationViewController* viewController = [[InfomationViewController alloc] init];
self.infomationViewController = viewController;
[self.view addSubview:viewController.view];
[viewController release];
UINavigationBar* bar = [[UINavigationBar alloc] init];
[self.view insertSubview:bar aboveSubview:self.infomationViewController.view];
self.navagationBar = bar;
[bar release];
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"About Password Generator"];
[bar pushNavigationItem:item animated:YES];
UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)];
UIBarButtonItem* rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)];
item.leftBarButtonItem = leftBarButton;
item.rightBarButtonItem = rightBarButton;
[item release];
[leftBarButton release];
[rightBarButton release];
}
- (void) removeInfomationView
{
[_infomationViewController.view removeFromSuperview];
[_infomationViewController release];
_infomationViewController = nil;
[_navagationBar removeFromSuperview];
[_navagationBar release];
_navagationBar = nil;
}
- (void) SwitchView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
if ([self.infomationButton superview])
{
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self removeMainView];
[UIView commitAnimations];
[self onInfomationView];
}
else
{
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self removeInfomationView];
[UIView commitAnimations];
[self onMainView];
}
[self reLayout];
}
- (void) removeMainView
{
[_infomationButton removeFromSuperview];
[_infomationButton release];
_infomationButton = nil;
[_mainViewController.view removeFromSuperview];
[_mainViewController release];
_mainViewController = nil;
}
- (void) onMainView
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[self.view addSubview:button];
[button addTarget:self action:@selector(SwitchView) forControlEvents:UIControlEventTouchUpInside];
self.infomationButton = button;
MainViewController* viewController = [[MainViewController alloc] init];
[self.view insertSubview:viewController.view belowSubview:self.infomationButton];
self.mainViewController = viewController;
[viewController release];
}
- (void) reLayout
{
CGPoint origin = self.view.frame.origin;
CGSize size = self.view.frame.size;
CGFloat w = 40;
CGFloat h = 40;
CGFloat yMargin = 10;
CGFloat xMargin = 10;
CGFloat x = origin.x + size.width-2*xMargin-w;
CGFloat y = origin.y + size.height - 2*yMargin - h;
_navagationBar.frame = CGRectMake(origin.x, origin.y+20, size.width, 40);
_infomationButton.frame = CGRectMake(x, y, w, h);
}
-(void) viewWillAppear:(BOOL)animated
{
[self reLayout];
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) dealloc
{
[self removeInfomationView];
[self removeMainView];
[super dealloc];
}
@end
帮助页面视图类
代码如下
//
// InfomationView.m
// PasswordGenerator
//
// Created by arbboter on 14/12/23.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import "InfomationView.h"
@interface InfomationView ()
@property (nonatomic, retain) UILabel* logoLabel;
@property (nonatomic, retain) UIImageView* bkImageView;
@end
@implementation InfomationView
- (id)init
{
self = [super init];
if(self == nil)
{
return self;
}
UILabel* label = nil;
label = [[UILabel alloc] init];
label.text = @"Copyright (c) 2014年 arbboter.";
label.textAlignment = NSTextAlignmentCenter;
self.logoLabel = label;
[self addSubview:label];
[label release];
UIImageView* imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"bk.jpg"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.bkImageView = imageView;
[self insertSubview:imageView belowSubview:self.logoLabel];
[imageView release];
return self;
}
- (void) reLayout
{
CGPoint origin = self.frame.origin;
CGSize size = self.frame.size;
CGFloat yMargin = 10;
CGFloat xMargin = 10;
CGFloat w = size.width-2*xMargin;
CGFloat h = 40;
CGFloat x = origin.x + xMargin;
CGFloat y = origin.y + size.height - 2*yMargin - h;
_bkImageView.frame = self.frame;
_logoLabel.frame = CGRectMake(x, y, w, h);
}
- (void) dealloc
{
[_bkImageView release];
[_logoLabel release];
[super dealloc];
}
@end