专栏首页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 条评论
登录 后参与评论

相关文章

  • 第123天:移动web开发中的常见问题

    准备编辑这段时发现简书上已经有作者写了关于rem布局的介绍,并且他的设置比我所用的更加简洁,贴上地址供大家学习参考。 手机端页面自适应解决方案—rem布局进阶...

    半指温柔乐
  • iOS屏幕旋转及其基本适配方法

    前段时间抽空总结了一下iOS视频播放的基本用法,发现这其中还有一个我们无法绕过的问题,那就是播放界面的旋转与适配。的确,视频播放与游戏类型的App经常会遇到这个...

    梧雨北辰
  • 微信iOS收款到账语音提醒开发总结

    一、背景 为了解决小商户老板们在频繁交易中不方便核对、确认到账的痛点,产品MM提出了新版本需要支持收款到账语音提醒功能。这篇文章总结了开发过程中遇到的坑和一些小...

    腾讯Bugly
  • iOS---开发实用传感器

    用户1941540
  • js调用原生API--陀螺仪和加速器

    介绍 W3C设备方向规范允许开发者使用陀螺仪和加速计的数据。这个功能能被用来在现代浏览器里构筑虚拟现实和增强现实的体验。但是这处理原生数据的学习曲线对开发者来说...

    前朝楚水
  • iOS开发常用之网络

    GuangdongQi
  • 理解Unity3D中的四种坐标体系

    2017-07-31 by Liuqingwen | Tags: Unity3D | Hits

    IT自学不成才
  • iOS开发技巧篇

    在iOS开发中,有一些技巧可以提高程序猿的开发效率。 1,Xcode真机调试 Xcode 7推出之前,想要真机调试,iOS开发者必须花$99购买苹果开发者账号,...

    xiangzhihong
  • iOS---iPad开发及iPad特有的特技

    用户1941540

扫码关注云+社区

领取腾讯云代金券