`

iPhone开发之Rotation

 
阅读更多

 

iPhone或iPad可以支持4种朝向
    UIInterfaceOrientationPortrait
    UIInterfaceOrientationPortraitUpsideDown
    UIInterfaceOrientationLandscapeLeft
    UIInterfaceOrientationLandscapeRight

究竟支持哪几个朝向,由view controller的shouldAutorotateToInterfaceOrientation函数来指定,每当设备的朝向发生变化时,这个方法都会被调用。

对于iPhone程序,应该防止用户在通话时拿倒电话,不支持UIInterfaceOrientationPortraitUpsideDown
因此函数实现为:
- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation) interfaceOrientation {
      return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
对于iPad,应该支持所有的朝向,因此函数实现为
- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation) interfaceOrientation {
      return true;
}

实现对Rotation的支持有三种方法:
1. 设置UI元素的Autosize Attribute,类似于.NET form的dock
2. 重新构造UI元素大小和位置
3. 不同的朝向使用不同的view

使用Autosize Attribute
image
Inner box中的红色箭头和UI元素的size有关,如果横向的红色的箭头成为实线,则窗口尺寸变化时UI元素的宽度也会变化,如果横向红色箭头为虚线,则窗口宽度变化时UI元素的宽度保持不变。垂直方向同理。
Inner box周围的短线变成实线时,表明UI元素和它所在的view的边缘的距离保持不变。

下图中的UI对象的高度将会随着view的高度而变化,同时UI元素相对于View的上下边距不变

imageimage

对应的UI设置为
image

重新构造UI元素的大小和位置
image

- (void)willAnimateRotationToInterfaceOrientation:  (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
   if (interfaceOrientation == UIInterfaceOrientationPortrait
        || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  {
      button1.frame = CGRectMake(20, 20, 125, 125);
      button2.frame = CGRectMake(175, 20, 125, 125);
      button3.frame = CGRectMake(20, 168, 125, 125);
      button4.frame = CGRectMake(175, 168, 125, 125);
      button5.frame = CGRectMake(20, 315, 125, 125);
      button6.frame = CGRectMake(175, 315, 125, 125);
   }
   else
   {
      button1.frame = CGRectMake(20, 20, 125, 125);
      button2.frame = CGRectMake(20, 155, 125, 125);
      button3.frame = CGRectMake(177, 20, 125, 125);
      button4.frame = CGRectMake(177, 155, 125, 125);
      button5.frame = CGRectMake(328, 20, 125, 125);
      button6.frame = CGRectMake(328, 155, 125, 125);
   }
}

切换View
imageimage
为了支持两个View,需要在View controll中生成两套outlet
@interface SwapViewController : UIViewController {
      UIView *landscape;
      UIView *portrait;
      // Foo
      UIButton *landscapeFooButton;
      UIButton *portraitFooButton;
      // Bar
      UIButton *landscapeBarButton;
      UIButton *portraitBarButton;
}
在Interface builder中删除Xcode自动生成的view,从library中拖两个view 到main window上。分别命名为Portrait和Landscape.
选中File’s Owner,Control-drag 到Portrait和Landscape,连接outlet

动态加载view
#define degreesToRadians(x) (M_PI * (x) / 180.0)

- (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));#define degreesToRadians(x) (M_PI * (x) / 180.0)
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
}


分享到:
评论

相关推荐

    iphone开发 动画类库

    iPhone开发中的动画类库提供了丰富的功能,使得开发者能够创建出各种动态效果,如旋转、翻滚和渐变。本资源是一个专注于iPhone开发的动画类库,包含了源代码,可以直接导入到你的iOS项目中使用,极大地简化了动画...

    iPhone常用代码集合

    在iOS开发中,经常需要对图片进行裁剪或处理。以下代码展示了如何使用`UIImage`和Core Graphics框架中的`CGImageCreateWithImageInRect`函数来获取一张图片的部分区域: ```swift let image = UIImage(named: ...

    史上最全的ios开发源码

    图表之Rotation Pie Chart 图表之实时更新的曲线图 图像类 图像(Image)Transition ImageView 图像(Image)之Colorized Progress View 图像-AsyncImageView 图像-Blurred Image 图像-iOS Image Editor 图像-...

    iPhone-Touch

    在iOS开发中,iPhone的触摸事件处理是用户体验设计的核心部分。`iPhone-Touch`这个主题主要涉及了如何在iPhone的应用程序中捕捉并响应用户的触摸操作,包括手势识别、触摸序列跟踪以及与用户交互的多种方式。以下是...

    IPHONE实现转盘

    在动画块内,更新`RoundDisk`的transform属性,使用`CGAffineTransform(rotationAngle:)`创建一个旋转变换。这里的参数是角度,可以是从当前角度到目标角度的差值。 4. **手势识别**:为了响应用户的旋转操作,可以...

    java 图片压缩 iphone拍照上传旋转问题处理压缩工具类

    在Java开发中,处理图像尤其是来自移动设备如iPhone的照片时,常常会遇到一些特定的问题,例如照片上传时的旋转问题。这是因为iOS设备在拍摄照片时可能会记录照片的元数据(Exif信息),其中包括照片的旋转角度,这...

    Apress.Beginning.iPhone.Development.Exploring.the.iOS.SDK.2014

    本书名为《Apress.Beginning.iPhone.Development.Exploring.the.iOS.SDK.2014》,该书旨在为初学者提供一个全面的指南,教授如何使用iOS 8 SDK及Objective-C来开发iPhone和iPad应用。本书的目标读者是对iOS应用开发...

    iphone 转盘菜单

    在iOS开发中,"iPhone转盘菜单"是一种常见的交互设计,它通过动态的圆形布局提供了一种独特的用户体验。这种菜单通常被设计成半圆形,能够展示多个选项,并且通过旋转动画来选择不同的功能。在本篇文章中,我们将...

    ios5.0 开发基础教程 源码

    本教程源码提供了全面的实例,涵盖了iPhone应用开发中的关键元素,包括UI设计和交互。以下是对这些知识点的详细解释: 1. **控件使用**:在iOS应用中,控件是用户与应用交互的基础。本教程涵盖的控件有: - **按钮...

    Pro.iPhone.Development.with.SwiftUI.4th.Edition

    通过阅读《Pro iPhone Development with SwiftUI 4th Edition》,开发者可以全面提高SwiftUI技能,不仅能够熟练地构建美观、功能丰富的应用程序,还能了解并遵循Apple的最新开发趋势。无论你是初学者还是有经验的iOS...

    (0093)-iOS/iPhone/iPAD/iPod源代码-视图切换(View Transition)-Flip Card NavigationView

    在iOS开发中,视图切换(View Transition)是构建用户界面和交互的重要组成部分。本示例项目"(0093)-iOS/iPhone/iPad/iPod源代码-视图切换(View Transition)-Flip Card NavigationView"专注于提供一个独特的...

    (0083)-iOS/iPhone/iPAD/iPod源代码-动画(Animation)-Core Animation Demos

    "(0083)-iOS/iPhone/iPAD/iPod源代码-动画(Animation)-Core Animation Demos"这个资源包,显然是为了帮助开发者深入理解和掌握Core Animation的各种使用技巧和应用场景。 Core Animation的核心概念主要包括...

    (0084)-iOS/iPhone/iPAD/iPod源代码-动画(Animation)-GraphicsAnimation

    "(0084)-iOS/iPhone/iPAD/iPod源代码-动画(Animation)-GraphicsAnimation"这个项目着重于如何实现图形和动画效果。下面将详细讨论相关知识点。 1. **图形变换(Graphics Transformation)**: - iOS中的`...

    (0073)-iOS/iPhone/iPAD/iPod源代码-视图切换(View Transition)-View Transition

    在iOS开发中,视图切换(View Transition)是构建用户界面和交互时不可或缺的一部分。它为应用程序提供了平滑、引人入胜的动画效果,帮助用户在不同的屏幕间导航。本资源“iOS/iPhone/iPAD/iPod源代码-视图切换...

    ios 图片旋转

    本文将深入探讨如何在iOS平台上实现在不同设备如iPhone和iPad上根据手势方向进行360度旋转图片的功能。 首先,我们需要引入相关的框架。在iOS开发中,主要使用UIKit框架来处理用户界面和图像操作。确保在你的项目中...

    IOS6基础(清晰版)

    第十一章"iPad Considerations"专注于iPad应用开发的特定考虑,由于iPad与iPhone的屏幕尺寸和用户交互方式不同,开发者需要对应用进行特别的适配和优化。 第十二章"Application Settings and User Defaults"讲解了...

    Android 模仿iPhone列表数据View刷新动画详解

    在Android开发中,为了提升用户体验,开发者经常需要模仿其他平台,如iPhone,的交互效果。本文将详细解析如何在Android中实现类似iPhone列表数据View的刷新动画。这些动画通常用于模拟列表视图在加载新数据或刷新...

    core animation cook book

    Core Animation 在 iPhone 和其他 Apple 设备上提供了高性能的图形渲染能力,并且与 Objective-C 编程语言紧密结合,使得开发者能够轻松实现各种动态效果。 #### 2. 图层(Layer)的概念 在 Core Animation 中,...

    高仿网易新闻头像旋转(iPhone源代码)

    这个项目"高仿网易新闻头像旋转(iPhone源代码)"提供了一个iPhone应用的源码,由开发者@盼叔叔贡献,采用自定义授权协议。 首先,我们来探讨Core Animation。Core Animation是Apple提供的一个低级图形渲染引擎,用于...

    ios-触摸手势 - demo.zip

    4. **旋转(Rotation Gesture)** 旋转手势用于识别用户旋转手指的动作,常用于调整图像或对象的角度。`UIRotationGestureRecognizer`类用于识别旋转,通过监听手势的旋转角度变化,可以改变相应元素的旋转角度。 ...

Global site tag (gtag.js) - Google Analytics