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

移动开发 iOS
iPhone开发中如何将制作图片放大缩小案例是本文要介绍的内容,主要是来学习iphone开发中动画的制作,具体内容一起来看本文详解。

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

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

动画效果请参照下图

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

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

最重要的动画代码

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

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

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

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

责任编辑:zhaolei 来源: CSDN博客
相关推荐

2011-08-15 15:44:46

iPhone开发PDF

2011-08-18 16:24:44

iPhone开发图片

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-18 16:03:34

iPhone上传图片

2011-08-19 10:05:30

iPhone开发

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-18 15:40:20

iPhone文本切页

2010-01-21 14:13:48

VB.NET制作图片按

2017-05-03 16:36:32

Android图片动画

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2020-08-12 09:14:45

Python验证码工具

2011-07-08 15:08:16

iPhone 图片

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-08-19 10:13:05

iPhone开发

2011-08-19 11:10:31

iPhone应用

2011-08-10 14:40:23

iPhone动画

2022-08-30 23:40:32

JavaScrip图表Chart.js

2011-08-16 14:54:12

iphone开发APP

2011-08-19 11:03:37

iPhone应用三轴感应器

2011-07-27 11:19:33

iPhone UITableVie
点赞
收藏

51CTO技术栈公众号