1:原文摘自:http://mobile.51cto.com/iphone-285108.htm

iPhone开发中如何将制作图片放大缩小案例是本文要介绍的内容,主要是来学习iphone开发动画的制作,具体内容一起来看本文详解。在IPhone SDK开发范例大全中,有很多的范例码。

下面这段范例码,示范了两张图片的交换,以及放大缩小的动画

动画效果请参照下图

  1. #import <UIKit/UIKit.h>
  2. #define IMAGE_VIEW_1    100
  3. #define    IMAGE_VIEW_2    101
  4. #define BIGRECT CGRectMake(0.0f, 0.0f, 320.0f, 435.0f)
  5. #define SMALLRECT CGRectMake(130.0f, 187.0f, 60.0f, 60.0f)
  6. @interface ToggleView: UIView
  7. {
  8. BOOL isOne;
  9. }
  10. @end
  11. @implementation ToggleView
  12. - (id) initWithFrame: (CGRect) aFrame;
  13. {
  14. self = [super initWithFrame:aFrame];
  15. // Load both views, make them non-interactive
  16. UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:BIGRECT];
  17. imgView1.image = [UIImage imageNamed:@"one.png"];
  18. imgView1.userInteractionEnabled = NO;
  19. imgView1.tag = IMAGE_VIEW_1;
  20. UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:SMALLRECT];
  21. imgView2.image = [UIImage imageNamed:@"two.png"];
  22. imgView2.userInteractionEnabled = NO;
  23. imgView2.tag = IMAGE_VIEW_2;
  24. // image 1 is in front of image 2 to begin
  25. [self addSubview:imgView2];
  26. [self addSubview:imgView1];
  27. isOne = YES;
  28. [imgView1 release];
  29. [imgView2 release];
  30. return self;
  31. }
  32. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  33. {
  34. // Determine which view occupies which role
  35. UIImageView *big = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_1 : IMAGE_VIEW_2)];
  36. UIImageView *little = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_2 : IMAGE_VIEW_1)];
  37. isOne = !isOne;
  38. // Pack all the changes into the animation block
  39. CGContextRef context = UIGraphicsGetCurrentContext();
  40. [UIView beginAnimations:nil context:context];
  41. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  42. [UIView setAnimationDuration:1.0];
  43. [big setFrame:SMALLRECT];
  44. [big setAlpha:0.5];
  45. [little setFrame:BIGRECT];
  46. [little setAlpha:1.0];
  47. [UIView commitAnimations];
  48. // Hide the shrunken "big" image.
  49. [big setAlpha:0.0f];
  50. [[big superview] bringSubviewToFront:big];
  51. }
  52. @end
  53. @interface HelloController : UIViewController
  54. @end
  55. @implementation HelloController
  56. - (void)loadView
  57. {
  58. ToggleView *contentView = [[ToggleView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  59. contentView.backgroundColor = [UIColor whiteColor];
  60. self.view = contentView;
  61. [contentView release];
  62. }
  63. @end
  64. @interface SampleAppDelegate : NSObject <UIApplicationDelegate>
  65. @end
  66. @implementation SampleAppDelegate
  67. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  68. UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  69. HelloController *hello = [[HelloController alloc] init];
  70. [window addSubview:hello.view];
  71. [window makeKeyAndVisible];
  72. }
  73. @end
  74. int main(int argc, char *argv[])
  75. {
  76. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  77. int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
  78. [pool release];
  79. return retVal;
  80. }
  81. #import <UIKit/UIKit.h>
  82. #define IMAGE_VIEW_1    100
  83. #define    IMAGE_VIEW_2    101
  84. #define BIGRECT CGRectMake(0.0f, 0.0f, 320.0f, 435.0f)
  85. #define SMALLRECT CGRectMake(130.0f, 187.0f, 60.0f, 60.0f)
  86. @interface ToggleView: UIView
  87. {
  88. BOOL isOne;
  89. }
  90. @end
  91. @implementation ToggleView
  92. - (id) initWithFrame: (CGRect) aFrame;
  93. {
  94. self = [super initWithFrame:aFrame];
  95. // Load both views, make them non-interactive
  96. UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:BIGRECT];
  97. imgView1.image = [UIImage imageNamed:@"one.png"];
  98. imgView1.userInteractionEnabled = NO;
  99. imgView1.tag = IMAGE_VIEW_1;
  100. UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:SMALLRECT];
  101. imgView2.image = [UIImage imageNamed:@"two.png"];
  102. imgView2.userInteractionEnabled = NO;
  103. imgView2.tag = IMAGE_VIEW_2;
  104. // image 1 is in front of image 2 to begin
  105. [self addSubview:imgView2];
  106. [self addSubview:imgView1];
  107. isOne = YES;
  108. [imgView1 release];
  109. [imgView2 release];
  110. return self;
  111. }
  112. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  113. {
  114. // Determine which view occupies which role
  115. UIImageView *big = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_1 : IMAGE_VIEW_2)];
  116. UIImageView *little = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_2 : IMAGE_VIEW_1)];
  117. isOne = !isOne;
  118. // Pack all the changes into the animation block
  119. CGContextRef context = UIGraphicsGetCurrentContext();
  120. [UIView beginAnimations:nil context:context];
  121. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  122. [UIView setAnimationDuration:1.0];
  123. [big setFrame:SMALLRECT];
  124. [big setAlpha:0.5];
  125. [little setFrame:BIGRECT];
  126. [little setAlpha:1.0];
  127. [UIView commitAnimations];
  128. // Hide the shrunken "big" image.
  129. [big setAlpha:0.0f];
  130. [[big superview] bringSubviewToFront:big];
  131. }
  132. @end
  133. @interface HelloController : UIViewController
  134. @end
  135. @implementation HelloController
  136. - (void)loadView
  137. {
  138. ToggleView *contentView = [[ToggleView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  139. contentView.backgroundColor = [UIColor whiteColor];
  140. self.view = contentView;
  141. [contentView release];
  142. }
  143. @end
  144. @interface SampleAppDelegate : NSObject <UIApplicationDelegate>
  145. @end
  146. @implementation SampleAppDelegate
  147. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  148. UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  149. HelloController *hello = [[HelloController alloc] init];
  150. [window addSubview:hello.view];
  151. [window makeKeyAndVisible];
  152. }
  153. @end
  154. int main(int argc, char *argv[])
  155. {
  156. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  157. int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
  158. [pool release];
  159. return retVal;
  160. }

最重要的动画代码

  1. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  2. {
  3. // 這一段代碼,設定目前哪一張圖是大圖,哪一張是小圖
  4. UIImageView *big = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_1 : IMAGE_VIEW_2)];
  5. UIImageView *little = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_2 : IMAGE_VIEW_1)];
  6. isOne = !isOne;
  7. // 這是使用動畫的一些基本設定
  8. CGContextRef context = UIGraphicsGetCurrentContext();
  9. [UIView beginAnimations:nil context:context];
  10. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 設定為IN OUT的動畫
  11. [UIView setAnimationDuration:1.0]; // 動畫時間為一秒
  12. [big setFrame:SMALLRECT];
  13. [big setAlpha:0.5];
  14. [little setFrame:BIGRECT];
  15. [little setAlpha:1.0];
  16. [UIView commitAnimations];
  17. // Hide the shrunken "big" image.
  18. [big setAlpha:0.0f];
  19. [[big superview] bringSubviewToFront:big];
  20. }
  21. @end
  22. - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  23. {
  24. // 這一段代碼,設定目前哪一張圖是大圖,哪一張是小圖
  25. UIImageView *big = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_1 : IMAGE_VIEW_2)];
  26. UIImageView *little = (UIImageView *)[self viewWithTag: (isOne ? IMAGE_VIEW_2 : IMAGE_VIEW_1)];
  27. isOne = !isOne;
  28. // 這是使用動畫的一些基本設定
  29. CGContextRef context = UIGraphicsGetCurrentContext();
  30. [UIView beginAnimations:nil context:context];
  31. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // 設定為IN OUT的動畫
  32. [UIView setAnimationDuration:1.0]; // 動畫時間為一秒
  33. [big setFrame:SMALLRECT];
  34. [big setAlpha:0.5];
  35. [little setFrame:BIGRECT];
  36. [little setAlpha:1.0];
  37. [UIView commitAnimations];
  38. // Hide the shrunken "big" image.
  39. [big setAlpha:0.0f];
  40. [[big superview] bringSubviewToFront:big];
  41. }
  42. @end

代码中设定透明度的目的,为了就是小图放大的时候,才不会被原本在上面的大图盖到,导致看不到图。

小结:iPhone开发中如何将制作图片放大缩小案例的内容介绍完了,希望通过本文的学习能对你有所帮助!

转载于:https://www.cnblogs.com/85538649/archive/2011/12/06/2278541.html

iphone iPhone开发中如何将制作图片放大缩小代码实现案例相关推荐

  1. 直播电商平台开发,RecyclerView实现item图片放大缩小

    直播电商平台开发,RecyclerView实现item图片放大缩小 ((ShopDetailsViewHolder) holder).img_list.setOnClickListener(new V ...

  2. [IPhone] 如何将制作图片放大缩小的动作

    在IPhone SDK开发范例大全中,有很多的范例码 下面这段范例码,示范了两张图片的交换,以及放大缩小的动画 动画效果请参照下图 #import <UIKit/UIKit.h> #def ...

  3. android动画view上移,在Android开发中使用View制作一个引导动画

    在Android开发中使用View制作一个引导动画 发布时间:2020-11-20 16:46:16 来源:亿速云 阅读:98 作者:Leah 这篇文章将为大家详细讲解有关在Android开发中使用V ...

  4. Android应用开发中三种常见的图片压缩方法

    Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 一.质量压缩法private Bitmap comp ...

  5. android打地鼠设计报告,android开发中利用handler制作一个打地鼠小游戏

    android开发中利用handler制作一个打地鼠小游戏 发布时间:2020-11-25 15:21:11 来源:亿速云 阅读:136 作者:Leah 这期内容当中小编将会给大家带来有关androi ...

  6. js吧html中的图片改为数组图片,巧用数组制作图片切换js代码

    巧用数组制作图片切换js代码 发布于 2017-07-05 07:15:55 | 104 次阅读 | 评论: 0 | 来源: 网友投递 JavaScript客户端脚本语言Javascript 是一种由 ...

  7. php中图片放大,jquery实现图片放大缩小特效

    和大家分享一个最近用到的jquery实现图片的放大缩小的特效.具体效果是鼠标移动到图片上,图片自动缩到左上角,右下角出现文字说明,鼠标移走后图片恢复,文字被图片遮挡.非常不错的一个图片特效功能.贴一下 ...

  8. imageView图片放大缩小及旋转

    imageView图片放大缩小及旋转 一.简介 二.方法 1)设置图片放大缩小效果 第一步:将<ImageView>标签中的android:scaleType设置为"fitCen ...

  9. 【Axure交互教程】滑块控制图片放大缩小效果

    作品名称:滑块控制图片放大缩小效果 作品编号:Case007 软件版本:Axure9 作品类型:交互案例 原型预览链接(附源文件下载链接):http://daisyaxure.com/demo/Cas ...

最新文章

  1. Androidx FloatingActionButton 中间图片颜色值修改
  2. vs 插件小番茄 visual assist x破解版 破解教程下载
  3. NSString属性什么时候用copy,什么时候用strong?
  4. 【TensorFlow】TFRecord数据集的制作:读取、显示及代码详解
  5. 日本的“电力路由器”概述
  6. CPU为什么不做成圆的而是方的?
  7. php容器原理,容器与依赖注入的原理
  8. 数据分析:星巴克店铺分布有何规律?
  9. 大量用户升级iPhone3.0系统导致苹果服务器故障
  10. python求解next数组实现KMP算法
  11. three.js 制作3D相册
  12. yaml文件的加载使用
  13. python绘制capm模型
  14. Android拨号盘列表加载机制(原)
  15. 【BFS】营救铁达尼号(C++)
  16. 程序模板(20210603笔记)
  17. mysql前缀索引 默认长度_如何确定前缀索引的长度?
  18. mysql密码为空包密码错误_apk空包签名方法及工具
  19. 【Hive】快速入门~
  20. 【MathType】彻底解决公式大小与文章文字大小不统一(含字体的字号与磅(pt)和像素(px)之间的换算关系)

热门文章

  1. java中replaceall用法_Java中String的ReplaceAll使用小结
  2. oracle什么时候使用静态监听,Oracle监听之动态监听与静态监听特点
  3. java请求超时异常捕获_我异常了,快来捕获我,Java异常简述
  4. vue快速复制快捷键_vue快捷键.doc
  5. oracle对某两列求和再求和_只会SUM函数求和,试试这5种求和函数,十倍提高工作效率...
  6. cdr圆形渐变填充怎么设置_玩出新花样|渐变应用于形状
  7. linux arm9开发环境,ARM9开发板Qt环境的搭建
  8. python sys模块详解_python中os和sys模块的区别与常用方法总结
  9. 收集了一些python的文章
  10. 贝叶斯集锦:MCMCpack包