专栏首页c#开发者IOS5开发-控件位置适应屏幕旋转代码

IOS5开发-控件位置适应屏幕旋转代码

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration {
    if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
        toOrientation == UIInterfaceOrientationLandscapeRight) {
        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationDuration:1.0];
        }
            [UIView setAnimationDelegate:self];
        [viewA setFrame:CGRectMake(?,?,?,?)];
        [viewB setFrame:CGRectMake(?,?,?,?)];
        [viewC setFrame:CGRectMake(?,?,?,?)];
        } [UIView commitAnimations];    
    } else if  (toOrientation == UIInterfaceOrientationPortrait ||
            toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [UIView beginAnimations:nil context:NULL]; {
            [UIView setAnimationDuration:1.0];
        }
        [UIView setAnimationDelegate:self];
        [viewA setFrame:CGRectMake(?,?,?,?)];
        [viewB setFrame:CGRectMake(?,?,?,?)];
        [viewC setFrame:CGRectMake(?,?,?,?)];
        } [UIView commitAnimations];    
    }   // Source @ http://www.jailbyte.ca/safari/files/SpinView.zip////  UntitledViewController.h//  SpinView////  Created by Richard Vacheresse on 10-05-20.//  Copyfright jailByte.ca 2010. Use it anyway you like.//#import <UIKit/UIKit.h>@interface UntitledViewController : UIViewController{	//-view (the image) used for manipulation by the accelerometer	UIImageView *imageView;}@property (nonatomic, retain) UIImageView *imageView;- (IBAction) actionBtnOne;@end---------------------------------------------------------------------------////  UntitledViewController.m////  SpinView - this is an example of how to deal with the rotation of views via the accelerometer and the//  overridden method shouldAutorotateToInterfaceOrientation. this example uses an image view as the view//	that is being rotated; this imageview is created with the press of a button, the resulting action is to//	simply show the layer in the view. from there you start to spin the device and the updates follow. to reset//  the view you must press the home key and then restart the app; sorry, just too lazy to add anymore.////	**note: there is a bug to this solution, and it becomes apparent when using the device and the user rotates//	the phone quickly 180 degrees. i believe it may have something to do with when the floats that are used//	to hold the x and y values are updated while rotating; like it grabs the coordinates while spinnning past 90//	degrees. the next time you rotate the view, the view will correct itself.////  Created by Richard Vacheresse on 10-05-20.//  Copyfright jailByte.ca 2010. Use it anyway you like.//#import "UntitledViewController.h"@implementation UntitledViewController@synthesize imageView;- (void)viewDidLoad{    [super viewDidLoad];		//-create a button	UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];	[btnOne setTitle: @"btnOne" forState: UIControlStateNormal];	[btnOne setFrame:CGRectMake(0.0f, 0.0f, 105.0f, 55.0f)];	[btnOne setCenter:CGPointMake(160.0f, 55.0f)];	[btnOne addTarget:self action:@selector(actionBtnOne) forControlEvents:UIControlEventTouchUpInside];	//-add it to the display	[self.view addSubview: btnOne];}////	actionBtnOne - initializes a UIImageView, sets the initial properties, add it to the display, then releases it.//	- (IBAction) actionBtnOne{	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];	[imageView setImage:[UIImage imageNamed:@"image.png"]];	[imageView setAutoresizesSubviews: YES];	[self.view addSubview:imageView];	[imageView release];}////	shouldAutorotateToInterfaceOrientation - used to interact with the accelerometer; this is an overriden method.//	- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{	if ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)	{				//-set the center of the image from the dimensions of the display		//-x value is +10 due to status bar		[UIView beginAnimations: nil context: NULL];		[UIView setAnimationDuration: 0.25];		CGFloat x = self.view.bounds.size.height / 2 + 10;		CGFloat y = self.view.bounds.size.width / 2;		CGPoint center = CGPointMake( x, y); 		[imageView setCenter: center];		[UIView commitAnimations];				[UIView beginAnimations: nil context: NULL];		[UIView setAnimationDuration: 0.25];				//-this will keep it in the same position - always standing up		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 0.5);				//this will keep it standing up as in portrait however it will turn it upsidedown		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 1.0);				//this will change the view to be upside-down but in proper alignment with the landscape mode		//CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);				//this will change the view to be rightside-up in proper alignment with landscape mode		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / -2.0);				imageView.transform = transform;		[UIView commitAnimations];			}	else	{		//-set the center of the image from the dimensions of the display		//-x value is +10 due to status bar		[UIView beginAnimations: nil context: NULL];		[UIView setAnimationDuration: 0.25];		CGFloat x = self.view.bounds.size.height / 2 + 10;		CGFloat y = self.view.bounds.size.width / 2;		CGPoint center = CGPointMake( x, y); 		[imageView setCenter: center];		[UIView commitAnimations];				[UIView beginAnimations: nil context: NULL];		[UIView setAnimationDuration: 0.25];		CGAffineTransform move = CGAffineTransformMakeTranslation(0.0f, 0.0f);		imageView.transform = move;		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 2);		imageView.transform = transform;		[UIView commitAnimations];	}	    // Return YES for supported orientations	return (interfaceOrientation == UIInterfaceOrientationPortrait ||			interfaceOrientation == UIInterfaceOrientationLandscapeRight ||			interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||			interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);}- (void)dealloc{	[imageView release];    [super dealloc];}//-the@end

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 解决:TF30170: Microsoft.ProjectCreationWizard.WorkItemTracking

    Error TF30170: The plugin Microsoft.ProjectCreationWizard.WorkItemTracking fail...

    阿新
  • Pass Multiple Values from a GridView to Another Page using ASP.NET

    Pass Multiple Values from a GridView to Another Page  using ASP.NET A common req...

    阿新
  • Add a FileUpload control to your GridView [转]

    Add a FileUpload control to your GridView Category:  ASP.Net 2.0 ...

    阿新
  • Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)

    A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes...

    Angel_Kitty
  • Ceph用户邮件列表Vol45-Issue3

    https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=af5e5...

    用户2772802
  • CodeForces 157A Game Outcome

    A. Game Outcome time limit per test 2 seconds memory limit per test 256 me...

    ShenduCC
  • 【神器】vimum在浏览器中键盘操作选择、复制、粘贴,键盘党的最爱

    阿炬
  • 机器人控制器编程课程-教案02-基础

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    zhangrelay
  • ZOJ 3202 Second-price Auction

    Time Limit: 1 Second      Memory Limit: 32768 KB

    ShenduCC
  • Kubernetes GC in V1.3 源码分析

    本文是对Kubernetes V1.3发布的新Garbage Collector模块的源码解读。实际上本文的是基于kubernetes v1.4的代码进行分析的...

    Walton

扫码关注云+社区

领取腾讯云代金券