initWithFrame 和 initWithCoder

initWithFrame  和   initWithCoder


当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化

- (id)initWithFrame:(CGRect)frame

{

    if (self =[super initWithFrame:frame]) {

        // 初始化代码

    }

    return self;

}


用于视图加载nib文件,从nib中加载对象实例时,使用 initWithCoder初始化这些实例对象

- (id)initWithCoder:(NSCoder*)coder

{

    if (self =[super initWithcoder:coder]) {

        // 初始化代码

    }

    return self;

}



1. initWithFrame方法是什么?

initWithFrame方法用来 初始化并返回一个新的视图对象,根据指定的CGRect(尺寸)。
当然,其他UI对象,也有initWithFrame方法,但是,我们以UIView为例,来搞清楚initWithFrame方法。

2.什么时候用initWithFrame方法?
简单的说,我们用编程方式申明,创建UIView对象时,使用initWithFrame方法。
在此,我们必须搞清楚,两种方式来进行初始化UIView。
1.使用 Interface Builder 方式。
这种方式,就是使用nib文件。通常我们说的“拖控件” 的方式。

实际编程中,我们如果用Interface Builder 方式创建了UIView对象。(也就是,用拖控件的方式)
那么,initWithFrame方法方法是不会被调用的。因为nib文件已经知道如何初始化该View。(因为,我们在拖该view的时候,就定义好了长、宽、背景等属性)。
这时候, 会调用initWithCoder方法,我们可以用initWithCoder方法来重新定义我们在nib中已经设置的各项属性。

2.使用编程方式。
就是我们声明一个UIView的子类,进行“手工”编写代码的方式。

实际编程中,我们使用编程方式下,来创建一个UIView或者创建UIView的子类。这时候,将调用initWithFrame方法,来实例化UIView。
特别注意,如果在子类中重载initWithFrame方法,必须先调用父类的initWithFrame方法。在对自定义的UIView子类进行初始化操作。
比如:

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];// 先调用父类的initWithFrame方法

    if (self) {

        

        // 再自定义该类(UIView子类)的初始化操作。

        _scrollView = [[UIScrollView allocinitWithFrame:self.bounds];

        [_scrollView setFrame:CGRectMake(00320480)];

        _scrollView.contentSize = CGSizeMake(320*3480);

        

        [self addSubview:_scrollView];

    }

    return self;

}


在这里,我想,应该对initWithFrame方法略知一二了。

那么,用Interface Builder 方式创建的nib文件是什么?

 

对于应用程序,资源是一种数据文件,伴随可程序执行程序的一种数据文件。(可以理解为可执行程序的,一种不可缺少的组陈部分)。

资源文件,是一种可移动的,由适合的工具编写的一种特殊的代码。

如:plish文件,txt文件,图像,视频等文件。都可以被xCode识别和引用。


一个应用程序可以包含多种形式的资源文件。


当然,nib文件也不例外,仅仅是一种资源文件。

通过Interface Builder 方式,可以创建nib文件,存储应用程序的UI对象。供应用程序来读取。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Cocoa 绘制动态折线图的代码示例,包含 .h 和 .m 文件: DynamicLineChart.h: ```objective-c #import <Cocoa/Cocoa.h> @interface DynamicLineChart : NSView @property (nonatomic, strong) NSColor *lineColor; // 折线颜色 @property (nonatomic, assign) CGFloat lineWidth; // 折线宽度 @property (nonatomic, assign) CGFloat pointRadius; // 数据点半径 @property (nonatomic, assign) NSTimeInterval animationDuration; // 动画时长 - (void)addDataPoint:(CGFloat)dataPoint; // 添加一个数据点 @end ``` DynamicLineChart.m: ```objective-c #import "DynamicLineChart.h" @interface DynamicLineChart() @property (nonatomic, strong) NSMutableArray *dataPoints; // 存储数据点的数组 @property (nonatomic, assign) CGFloat maxValue; // 数据最大值 @property (nonatomic, assign) CGFloat minValue; // 数据最小值 @property (nonatomic, assign) CGFloat valueRange; // 数据值范围 @end @implementation DynamicLineChart - (instancetype)initWithFrame:(NSRect)frameRect { if (self = [super initWithFrame:frameRect]) { [self setup]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder { if (self = [super initWithCoder:coder]) { [self setup]; } return self; } - (void)setup { self.dataPoints = [NSMutableArray array]; self.lineColor = [NSColor blackColor]; self.lineWidth = 2.0; self.pointRadius = 3.0; self.animationDuration = 0.5; } - (void)addDataPoint:(CGFloat)dataPoint { // 将数据点添加到数组中 [self.dataPoints addObject:@(dataPoint)]; // 更新最大值和最小值 if (self.dataPoints.count == 1) { self.maxValue = dataPoint; self.minValue = dataPoint; } else { self.maxValue = MAX(dataPoint, self.maxValue); self.minValue = MIN(dataPoint, self.minValue); } self.valueRange = self.maxValue - self.minValue; // 刷新视图 [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; if (self.dataPoints.count == 0) { return; } // 绘制折线 NSBezierPath *path = [[NSBezierPath alloc] init]; [[self.lineColor colorWithAlphaComponent:0.8] set]; path.lineWidth = self.lineWidth; CGFloat xInterval = self.bounds.size.width / (self.dataPoints.count - 1); for (NSInteger i = 0; i < self.dataPoints.count; i++) { CGFloat dataPoint = [self.dataPoints[i] floatValue]; CGFloat y = (dataPoint - self.minValue) / self.valueRange * self.bounds.size.height; CGFloat x = i * xInterval; if (i == 0) { [path moveToPoint:CGPointMake(x, y)]; } else { [path lineToPoint:CGPointMake(x, y)]; } } [path stroke]; // 绘制数据点 for (NSInteger i = 0; i < self.dataPoints.count; i++) { CGFloat dataPoint = [self.dataPoints[i] floatValue]; CGFloat y = (dataPoint - self.minValue) / self.valueRange * self.bounds.size.height; CGFloat x = i * xInterval; NSRect pointRect = NSMakeRect(x - self.pointRadius, y - self.pointRadius, self.pointRadius * 2, self.pointRadius * 2); NSBezierPath *pointPath = [NSBezierPath bezierPathWithOvalInRect:pointRect]; [[self.lineColor colorWithAlphaComponent:0.8] set]; [pointPath fill]; } } - (void)animate { if (self.dataPoints.count == 0) { return; } // 将视图高度缩小到 0 NSRect startRect = self.bounds; NSRect endRect = NSMakeRect(startRect.origin.x, startRect.origin.y, startRect.size.width, 0); CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"bounds"]; animation1.fromValue = [NSValue valueWithRect:startRect]; animation1.toValue = [NSValue valueWithRect:endRect]; animation1.duration = self.animationDuration / 2; animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; animation1.removedOnCompletion = NO; animation1.fillMode = kCAFillModeForwards; [self.layer addAnimation:animation1 forKey:@"animation1"]; // 更新数据并刷新视图 CGFloat lastDataPoint = [[self.dataPoints lastObject] floatValue]; [self.dataPoints removeAllObjects]; [self addDataPoint:lastDataPoint]; // 将视图高度恢复 CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"bounds"]; animation2.fromValue = [NSValue valueWithRect:endRect]; animation2.toValue = [NSValue valueWithRect:startRect]; animation2.duration = self.animationDuration / 2; animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; animation2.beginTime = CACurrentMediaTime() + self.animationDuration / 2; animation2.removedOnCompletion = NO; animation2.fillMode = kCAFillModeForwards; [self.layer addAnimation:animation2 forKey:@"animation2"]; } @end ``` 使用时,可以创建一个 DynamicLineChart 的实例,并调用 addDataPoint 方法添加数据点。如果需要动态更新折线图,可以调用 animate 方法实现动画效果。例如: ```objective-c DynamicLineChart *lineChart = [[DynamicLineChart alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; [lineChart addDataPoint:10.0]; [lineChart addDataPoint:20.0]; [lineChart addDataPoint:30.0]; [lineChart animate]; ``` 这里只是一个简单的示例,具体实现还需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值