iOS-UI篇—简单的浏览器查看程序和Tomcat简单实现

时间:2023-03-09 21:27:26
iOS-UI篇—简单的浏览器查看程序和Tomcat简单实现
 #import "ViewController.h"

 @interface ViewController ()

 @property (retain, nonatomic) NSArray *pic;
@property (assign, nonatomic) NSInteger index;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet UIImageView *picture;
- (IBAction)nextPicture:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnnext; - (IBAction)lastPicture:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnLast; @end
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//让第一个图片显示出来,所以先调用一次
[self nextPicture:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} // 重写pic属性get方法---懒加载数据
- (NSArray *)pic
{ /*
写代码加载picture.plist文件中的数据到_pic;
1.获取picture.plist文件的路径
ps:[NSBundle mainBundle]获取这个app安装到手机上时的根目录
然后在根目录下搜索picture.plist文件路径
*/
if (_pic == nil) {
NSString *string = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@".plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:string];
//NSLog(@"%@",array); _pic = array; }
return _pic;
} - (IBAction)nextPicture:(id)sender {
//1.让索引自加
_index++;
//2.从数组中获取当前这张图片的数据
NSDictionary *dict = self.pic[self.index-];
//3.把获取到得数据设置给界面上的控件
self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];
//4.通过image属性来设置图片框里面的图片
self.picture.image =[UIImage imageNamed: dict[@"picture"]]; self.lblTitle.text = dict[@"title"];
//设置“下一张”按钮是否可以点击
self.btnnext.enabled =( _index != self.pic.count);
//设置“上一张”按钮是否可以点击
self.btnLast.enabled =( _index- != ); } - (IBAction)lastPicture:(id)sender {
_index--;
NSDictionary *dict = self.pic[self.index-];
self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];
self.picture.image =[UIImage imageNamed: dict[@"picture"]];
self.lblTitle.text = dict[@"title"]; self.btnLast.enabled =( _index- != );
self.btnnext.enabled =( _index != self.pic.count);
}
@end

开发思路:

1.完成基本功能

2.考虑性能

(1)(初始化操作,可以直接调用change进行)

(2)因为要控制序号和图片两个变量,所以考虑使用字典代替掉switch

(3)每次点击,字典都需要创建一次,效率地下,可以考虑创建的这部分拿到初始化方法中去,这样就只需要创建一次就ok了。

(4)考虑缺点(对代码的顺序要求极其严格)

(5)懒加载(需要的时候才加载,那么什么时候是需要的时候,及调用get方法的时候)

(6)每次都来一下?效率低下—》只有第一次调用get方法时为空,此时实例化并建立数组,其他时候直接返回成员变量(仅仅执行一次)

注意点:

1.方法的调用堆栈(顺序)。

2.使用plist:让数据的操作更加灵活,把数据弄到外面去,解除耦合性,让耦合性不要太强。实际上是一个xml,是苹果定义的一种特殊格式的xml。

3.bundle-包(只读)

二、Tomcat简单实现(吃小鸟)

 - (IBAction)eatBird:(id)sender {

     [self startAnimating: picName:@"cat_eat"];
} - (void)startAnimating:(int)count picName:(NSString *)picName
{
// 如果当前图片框正在执行动画,那么直接return,什么也不做
if (self.imageViewCat.isAnimating) {
return;
}
// 1.加载图片到数组中
NSMutableArray *array = [NSMutableArray array];
for (int i=; i<count; i++) {
// 拼接图片名称
NSString *imagName = [NSString stringWithFormat:@"%@%04d.jpg",picName,i];
/*
根据图片名称加载图片
通过imageNamed:这种方式加载图片,加载好的图片会一直保存写在内存中,不会释放。
优点:下次如果再使用同样的图片时就不需要重新加载了,因为内存也已有
缺点:如果加载大量的图片,那么这些图片会一直保存在内存中,导致应用程序占用内存过大(这叫缓存)
//UIImage *imageCat = [UIImage imageNamed:imagName];
*/ // 解决:换一种加载图片的方式,不需要使用缓存
// 获取图片完整的路径
NSString *path = [[NSBundle mainBundle] pathForResource:imagName
ofType:nil];
// 这里的参数不能传递图片名称了,这里需要传递图片的完整路径
UIImage *imageCat = [UIImage imageWithContentsOfFile:path]; // 把图片加载到数组中
[array addObject:imageCat];
}
// 2.设置图片的UIImageView(图片框)的animationimage属性
self.imageViewCat.animationImages = array;
// 3.设置动画持续时间
self.imageViewCat.animationDuration = self.imageViewCat.animationImages.count * 0.1; // 4.设置动画是否重播
self.imageViewCat.animationRepeatCount = ;
// 5.开启动画
[self.imageViewCat startAnimating]; /*
清空图片集合
这样写的问题是,当动画启动以后,动画还没有执行,就已经让图片集合清空了,也就是说
self.imageViewCat.animationImages里面已经没有图片了,所以动画就不执行了。
self.imageViewCat.animationImages = nil;
想法:需要延迟一段时间执行,当动画执行完毕后再清空这些图片
*/ //设置图片框在调用setAnimationImages:nil方法的时候延迟执行
[self.imageViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageViewCat.animationImages.count*0.1];
}