- iOS 学习笔记(一)xcode 4.3.2 下创建第一个 iOS 项目
- iOS 学习笔记(二)xcode 4.3.2 下实现基本交互
- iOS 学习笔记(三)UISlider 与 UISwitch 控件
- iOS 学习笔记(四)收回软键盘的两种方式
- iOS 学习笔记(五)xcode4.3.2 中添加 Window-Based Application 模版
- iOS 学习笔记(六)使用 UIScrollView 嵌套 UIView 子类
- Swift 快速入门(一)第一个 Swift 程序
- Swift 快速入门(二)基本数据类型
- Swift 快速入门(三)运算符
- Swift 快速入门(四)流程控制
- Swift 快速入门(五)集合
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
iOS 学习笔记(六)使用 UIScrollView 嵌套 UIView 子类
完成这一功能的前提是,你应该先安装好我上一节所说道的 window-Based Application 模版:
教程地址: http://blog.csdn.net/itachi85/article/details/7706549
接着我们要新建一个 window-Based Application 模版
我们创建一个名为 HypnosisView 的 objetctive-C class 文件:
HypnosisView.h:
#import <Foundation/Foundation.h>
@interface HypnosisView : UIView
{
}
@end
HypnosisView.m:
在这个文件中我们加入了绘制了一个同心圆的图形
#import "HypnosisView.h"
@implementation HypnosisView
- (void)drawRect:(CGRect)rect
{
// 获取绘图区域
CGRect bounds = [self bounds];
// 计算中心点
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
// 计算中心点至边界角的距离
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
// 获取绘图所需要的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 用 10 点的宽度来绘制所有的线条
CGContextSetLineWidth(context, 10);
// 线条颜色设为淡灰
[[UIColor lightGrayColor] setStroke];
// 绘制同心圆
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
CGContextAddArc(context, center.x, center.y,
currentRadius, 0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
}
NSString *text = @"hi: i am henrymorgen.";
// 获取绘图所需要的字体
UIFont *font = [UIFont boldSystemFontOfSize:28];
// 计算绘图位置
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;
// 将当前上下文的填充色设为黑色
[[UIColor blackColor] setFill];
[text drawInRect:textRect
withFont:font];
}
@end
接着我们配置代理 AppDelegate.h:
#import <UIKit/UIKit.h>
@class HypnosisView;
@interface HypnosisterAppDelegate : NSObject
<UIApplicationDelegate, UIScrollViewDelegate> {
HypnosisView *view;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
AppDelegate.m:
在代理中我们添加了 ScrollView,并将先前的 HypnosisView 添加到 ScrollView 中
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
@implementation HypnosisterAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect wholeWindow = [[self window] bounds];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
[[self window] addSubview:scrollView];
// 将视图的大小设为窗口的二倍
CGRect reallyBigRect;
reallyBigRect.origin = CGPointZero;
reallyBigRect.size.width = wholeWindow.size.width * 2.0;
reallyBigRect.size.height = wholeWindow.size.height * 2.0;
[scrollView setContentSize:reallyBigRect.size];
// 在 UIScrollView 对象里居中
CGPoint offset;
offset.x = wholeWindow.size.width * 0.5;
offset.y = wholeWindow.size.height * 0.5;
[scrollView setContentOffset:offset];
// 启用缩放功能
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:5];
[scrollView setDelegate:self];
// 创建视图
view = [[HypnosisView alloc] initWithFrame:reallyBigRect];
[view setBackgroundColor:[UIColor clearColor]];
[scrollView addSubview:view];
[scrollView release];
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];
[[self window] makeKeyAndVisible];
return YES;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view;
}
- (void)dealloc
{
[view release];
[_window release];
[super dealloc];
}
@end
最后运行一下看下效果:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论