当前位置:首页 > 技术 > 正文内容

图片浏览与Tom猫动画实现

访客 技术 2026年10月5日 1

图像变换与按钮交互

通过修改按钮的transform属性实现图像的动态效果。

缩放操作


- (IBAction)scaleButton:(UIButton *)btn {
    CGRect frame = self.imageView.frame;
    CGFloat factor = btn.isSelected ? 1.2f : 0.8f;
    frame.size.width *= factor;
    frame.size.height *= factor;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.imageView.frame = frame;
    }];
}

位移变换


CGAffineTransform move = CGAffineTransformTranslate(self.imageView.transform, 0, -30);
self.imageView.transform = move;
NSLog(@"%@", NSStringFromCGAffineTransform(self.imageView.transform));

旋转效果


CGFloat rotation = btn.isSelected ? -M_PI_4 : M_PI_4;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation);

动态按钮创建


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建系统按钮
    UIButton *sysBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sysBtn.frame = CGRectMake(20, 20, 100, 40);
    [sysBtn setTitle:@"点击" forState:UIControlStateNormal];
    [self.view addSubview:sysBtn];
    
    // 创建自定义按钮
    UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 100, 40)];
    customBtn.backgroundColor = [UIColor lightGrayColor];
    [customBtn setBackgroundImage:[UIImage imageNamed:@"btn_normal"] forState:UIControlStateNormal];
    [customBtn setBackgroundImage:[UIImage imageNamed:@"btn_pressed"] forState:UIControlStateHighlighted];
    [self.view addSubview:customBtn];
}

图像切换界面

实现左右按钮控制图片浏览功能。


- (NSArray *)imageData {
    if (!_imageData) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Gallery" ofType:@"plist"];
        _imageData = [NSArray arrayWithContentsOfFile:path];
    }
    return _imageData;
}

- (void)updateDisplay {
    NSDictionary *item = self.imageData[self.currentIndex];
    self.imageView.image = [UIImage imageNamed:item[@"imageName"]];
    self.titleLabel.text = item[@"description"];
    
    self.leftBtn.enabled = self.currentIndex > 0;
    self.rightBtn.enabled = self.currentIndex < self.imageData.count - 1;
}

内存管理实践

强引用与弱引用的区别:

  • 强引用:__strong Person *person = [[Person alloc] init];
  • 弱引用:__weak Person *weakSelf = person;

自动释放机制确保无强引用对象被回收。

Tom猫动画实现


- (IBAction)playAnimation:(UIButton *)sender {
    if (self.catImageView.isAnimating) return;
    
    NSMutableArray *frames = [NSMutableArray array];
    for (int i = 0; i < 25; i++) {
        NSString *filename = [NSString stringWithFormat:@"cat_%02d.jpg", i];
        NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
        [frames addObject:[UIImage imageWithContentsOfFile:path]];
    }
    
    self.catImageView.animationImages = frames;
    self.catImageView.animationDuration = 2.0;
    [self.catImageView startAnimating];
    
    [self performSelector:@selector(clearAnimation) withObject:nil afterDelay:self.catImageView.animationDuration];
}

- (void)clearAnimation {
    self.catImageView.animationImages = nil;
}
返回列表

上一篇:Pandas数据结构:Series详解

没有最新的文章了...

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

linux screen 用法详情 (nohup 的替代方案)

一、screen 是什么?能干嘛?screen 是一个终端复用器,可以:在一个 SSH 会话中开多个“虚拟终端”SSH 断线后,程序仍然在后台运行随时重新连接到原来的会话特别适合:nohup 的替代方案跑脚本 / 爬虫 / 训练模型运维、远程开发二、安装 screen# CentOS / Rocky / Almayum install -y screen# Debian / Ubuntuapt i...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。