Iphone 将UIViewController代码转换为UIView
我有一个基于动画的应用程序。经过数小时的尝试和搜索,最有效的动画处理器(具有低延迟和最佳内存管理)我发现了惊人的PNGAnimator(http://www.modejong.com/iPhone/). 有趣的是,我实际上使用的是JPEG(由于大小),但这不是问题所在 问题在于,该类本身是UIViewController的一个子类,当我将其添加到我的rootViewController时,新的视图控制器与它重叠,因此所有的界面元素(按钮…)都隐藏在它下面 这就是为什么我想以某种方式将代码转换为UIView的子类。(因此我可以在上面添加按钮)我尝试自己转换代码,但是应用程序会在动画调用时崩溃。你能告诉我我应该把代码改成什么,这样它就可以作为UIView了吗 例如,我尝试在.h文件中将其更改为UIView,然后在.m文件中,将references self.view更改为just self。然后在我的根视图控制器中将其添加为子视图,但这不会显示并崩溃 以下是.h文件:Iphone 将UIViewController代码转换为UIView,iphone,objective-c,xcode,uiview,uiviewcontroller,Iphone,Objective C,Xcode,Uiview,Uiviewcontroller,我有一个基于动画的应用程序。经过数小时的尝试和搜索,最有效的动画处理器(具有低延迟和最佳内存管理)我发现了惊人的PNGAnimator(http://www.modejong.com/iPhone/). 有趣的是,我实际上使用的是JPEG(由于大小),但这不是问题所在 问题在于,该类本身是UIViewController的一个子类,当我将其添加到我的rootViewController时,新的视图控制器与它重叠,因此所有的界面元素(按钮…)都隐藏在它下面 这就是为什么我想以某种方式将代码转换为U
//
// ImageAnimatorViewController.h
// PNGAnimatorDemo
//
// Created by Moses DeJong on 2/5/09.
//
#import <UIKit/UIKit.h>
#define ImageAnimator15FPS (1.0/15)
#define ImageAnimator12FPS (1.0/12)
#define ImageAnimator25FPS (1.0/24)
#define ImageAnimator18FPS (1.0/18)
#define ImageAnimatorDidStartNotification @"ImageAnimatorDidStartNotification"
#define ImageAnimatorDidStopNotification @"ImageAnimatorDidStopNotification"
@class AVAudioPlayer;
@interface ImageAnimatorViewController : UIViewController {
@public
NSArray *animationURLs;
NSTimeInterval animationFrameDuration;
NSInteger animationNumFrames;
NSInteger animationRepeatCount;
UIImageOrientation animationOrientation;
NSURL *animationAudioURL;
AVAudioPlayer *avAudioPlayer;
@private
UIImageView *imageView;
NSArray *animationData;
NSTimer *animationTimer;
NSInteger animationStep;
NSTimeInterval animationDuration;
NSTimeInterval lastReportedTime;
}
// public properties
@property (nonatomic, copy) NSArray *animationURLs;
@property (nonatomic, assign) NSTimeInterval animationFrameDuration;
@property (nonatomic, readonly) NSInteger animationNumFrames;
@property (nonatomic, assign) NSInteger animationRepeatCount;
@property (nonatomic, assign) UIImageOrientation animationOrientation;
@property (nonatomic, retain) NSURL *animationAudioURL;
@property (nonatomic, retain) AVAudioPlayer *avAudioPlayer;
@property (nonatomic, assign) CGRect viewCGRect;
// private properties
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, copy) NSArray *animationData;
@property (nonatomic, retain) NSTimer *animationTimer;
@property (nonatomic, assign) NSInteger animationStep;
@property (nonatomic, assign) NSTimeInterval animationDuration;
+ (ImageAnimatorViewController*) imageAnimatorViewController;
- (void) startAnimating;
- (void) stopAnimating;
- (BOOL) isAnimating;
- (void) animationShowFrame: (NSInteger) frame;
+ (NSArray*) arrayWithNumberedNames:(NSString*)filenamePrefix
rangeStart:(NSInteger)rangeStart
rangeEnd:(NSInteger)rangeEnd
suffixFormat:(NSString*)suffixFormat;
+ (NSArray*) arrayWithResourcePrefixedURLs:(NSArray*)inNumberedNames;
@end
//
//ImageAnimatorViewController.h
//PNGAnimatorDemo
//
//由摩西·德容于2009年2月5日创作。
//
#进口
#定义ImageAnimator15FPS(1.0/15)
#定义ImageAnimator12FPS(1.0/12)
#定义ImageAnimator25FPS(1.0/24)
#定义ImageAnimator18FPS(1.0/18)
#定义ImageAnimatorIDStartNotification@“ImageAnimatorIDStartNotification”
#定义ImageAnimatordStopNotification@“ImageAnimatordStopNotification”
@类音频层;
@界面图像AnimatorViewController:UIViewController{
@公开的
NSArray*动画URL;
NSTimeInterval animationFrameDuration;
NSInteger动画numframes;
NSInteger动画重复计数;
图像定向;动画定向;
NSURL*动画音频;
AVAudioPlayer*AVAudioPlayer;
@私人的
UIImageView*图像视图;
NSArray*动画数据;
NSTimer*动画定时器;
NSInteger动画步骤;
NSTimeInterval动画持续时间;
NSTimeInterval lastReportedTime;
}
//公共财产
@属性(非原子,复制)NSArray*animationURL;
@属性(非原子,赋值)NSTimeInterval animationFrameDuration;
@属性(非原子,只读)NSInteger animationNumFrames;
@属性(非原子,赋值)NSInteger animationRepeatCount;
@属性(非原子,赋值)UIImageOrientation animationOrientation;
@属性(非原子,保留)NSURL*animationAudioURL;
@属性(非原子,保留)AVAudioPlayer*AVAudioPlayer;
@属性(非原子,赋值)CGRect viewCGRect;
//私有财产
@属性(非原子,保留)UIImageView*imageView;
@属性(非原子,复制)NSArray*animationData;
@属性(非原子,保留)NSTimer*animationTimer;
@属性(非原子,赋值)NSInteger animationStep;
@属性(非原子,赋值)NSTimeInterval animationDuration;
+(ImageAnimatorViewController*)ImageAnimatorViewController;
-(无效)启动;
-(无效)停止设置动画;
-(BOOL)使人生气;
-(void)animationShowFrame:(NSInteger)frame;
+(NSArray*)带有NumberedNames的数组:(NSString*)文件名前缀
rangeStart:(NSInteger)rangeStart
rangeEnd:(NSInteger)rangeEnd
后缀格式:(NSString*)后缀格式;
+(NSArray*)数组,其资源前缀durls:(NSArray*)位于数字名称中;
@结束
这里是.m文件:
//
// ImageAnimatorViewController.m
// PNGAnimatorDemo
//
// Created by Moses DeJong on 2/5/09.
//
#import "ImageAnimatorViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioPlayer.h>
@implementation ImageAnimatorViewController
@synthesize animationURLs, animationFrameDuration, animationNumFrames, animationRepeatCount,
imageView, animationData, animationTimer, animationStep, animationDuration, animationOrientation, viewCGRect;
@synthesize animationAudioURL, avAudioPlayer;
- (void)dealloc {
// This object can't be deallocated while animating, this could
// only happen if user code incorrectly dropped the last ref.
NSAssert([self isAnimating] == FALSE, @"dealloc while still animating");
self.animationURLs = nil;
self.imageView = nil;
self.animationData = nil;
self.animationTimer = nil;
[super dealloc];
}
+ (ImageAnimatorViewController*) imageAnimatorViewController
{
return [[[ImageAnimatorViewController alloc] init] autorelease];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *myView = [[UIView alloc] initWithFrame:viewCGRect];
[myView autorelease];
self.view = myView;
/*UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[myView autorelease];
self.view = myView;
// FIXME: Additional Supported Orientations
if (animationOrientation == UIImageOrientationUp) {
// No-op
} else if (animationOrientation == UIImageOrientationLeft) {
// 90 deg CCW
//[self rotateToLandscape];
} else if (animationOrientation == UIImageOrientationRight) {
// 90 deg CW
//[self rotateToLandscapeRight];
} else {
NSAssert(FALSE,@"Unsupported animationOrientation");
}
*/
// Foreground animation images
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
[myImageView autorelease];
self.imageView = myImageView;
// Animation data should have already been loaded into memory as a result of
// setting the animationURLs property
NSAssert(animationURLs, @"animationURLs was not defined");
NSAssert([animationURLs count] > 1, @"animationURLs must include at least 2 urls");
NSAssert(animationFrameDuration, @"animationFrameDuration was not defined");
// Load animationData by reading from animationURLs
NSMutableDictionary *dataDict = [NSMutableDictionary dictionaryWithCapacity:[animationURLs count]];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:[animationURLs count]];
for ( NSURL* aURL in animationURLs ) {
NSString *urlKey = aURL.path;
NSData *dataForKey = [dataDict objectForKey:urlKey];
if (dataForKey == nil) {
dataForKey = [NSData dataWithContentsOfURL:aURL];
NSAssert(dataForKey, @"dataForKey");
[dataDict setObject:dataForKey forKey:urlKey];
}
[muArray addObject:dataForKey];
}
self.animationData = [NSArray arrayWithArray:muArray];
int numFrames = [animationURLs count];
float duration = animationFrameDuration * numFrames;
self->animationNumFrames = numFrames;
self.animationDuration = duration;
[self.view addSubview:imageView];
// Display first frame of image animation
self.animationStep = 0;
[self animationShowFrame: animationStep];
self.animationStep = animationStep + 1;
if (animationAudioURL != nil) {
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:animationAudioURL
error:nil];
[avPlayer autorelease];
NSAssert(avPlayer, @"AVAudioPlayer could not be allocated");
self.avAudioPlayer = avPlayer;
[avAudioPlayer prepareToPlay];
}
}
// Create an array of file/resource names with the given filename prefix,
// the file names will have an integer appended in the range indicated
// by the rangeStart and rangeEnd arguments. The suffixFormat argument
// is a format string like "%02i.png", it must format an integer value
// into a string that is appended to the file/resource string.
//
// For example: [createNumberedNames:@"Image" rangeStart:1 rangeEnd:3 rangeFormat:@"%02i.png"]
//
// returns: {"Image01.png", "Image02.png", "Image03.png"}
+ (NSArray*) arrayWithNumberedNames:(NSString*)filenamePrefix
rangeStart:(NSInteger)rangeStart
rangeEnd:(NSInteger)rangeEnd
suffixFormat:(NSString*)suffixFormat
{
NSMutableArray *numberedNames = [[NSMutableArray alloc] initWithCapacity:40];
for (int i = rangeStart; i <= rangeEnd; i++) {
NSString *suffix = [NSString stringWithFormat:suffixFormat, i];
NSString *filename = [NSString stringWithFormat:@"%@%@", filenamePrefix, suffix];
[numberedNames addObject:filename];
}
NSArray *newArray = [NSArray arrayWithArray:numberedNames];
[numberedNames release];
return newArray;
}
// Given an array of resource names (as returned by arrayWithNumberedNames)
// create a new array that contains these resource names prefixed as
// resource paths and wrapped in a NSURL object.
+ (NSArray*) arrayWithResourcePrefixedURLs:(NSArray*)inNumberedNames
{
NSMutableArray *URLs = [[NSMutableArray alloc] initWithCapacity:[inNumberedNames count]];
NSBundle* appBundle = [NSBundle mainBundle];
for ( NSString* path in inNumberedNames ) {
NSString* resPath = [appBundle pathForResource:path ofType:nil];
NSURL* aURL = [NSURL fileURLWithPath:resPath];
[URLs addObject:aURL];
}
NSArray *newArray = [NSArray arrayWithArray:URLs];
[URLs release];
return newArray;
}
// Invoke this method to start the animation
- (void) startAnimating
{
self.animationTimer = [NSTimer timerWithTimeInterval: animationFrameDuration
target: self
selector: @selector(animationTimerCallback:)
userInfo: NULL
repeats: TRUE];
[[NSRunLoop currentRunLoop] addTimer: animationTimer forMode: NSDefaultRunLoopMode];
animationStep = 0;
if (avAudioPlayer != nil)
[avAudioPlayer play];
// Send notification to object(s) that regestered interest in a start action
[[NSNotificationCenter defaultCenter]
postNotificationName:ImageAnimatorDidStartNotification
object:self];
}
// Invoke this method to stop the animation, note that this method must not
// invoke other methods and it must cancel any pending callbacks since
// it could be invoked in a low-memory situation or when the object
// is being deallocated. Invoking this method will not generate a
// animation stopped notification, that callback is only invoked when
// the animation reaches the end normally.
- (void) stopAnimating
{
if (![self isAnimating])
return;
[animationTimer invalidate];
self.animationTimer = nil;
animationStep = animationNumFrames - 1;
[self animationShowFrame: animationStep];
if (avAudioPlayer != nil) {
[avAudioPlayer stop];
avAudioPlayer.currentTime = 0.0;
self->lastReportedTime = 0.0;
}
// Send notification to object(s) that regestered interest in a stop action
[[NSNotificationCenter defaultCenter]
postNotificationName:ImageAnimatorDidStopNotification
object:self];
}
- (BOOL) isAnimating
{
return (animationTimer != nil);
}
// Invoked at framerate interval to implement the animation
- (void) animationTimerCallback: (NSTimer *)timer {
if (![self isAnimating])
return;
NSTimeInterval currentTime;
NSUInteger frameNow;
if (avAudioPlayer == nil) {
self.animationStep += 1;
// currentTime = animationStep * animationFrameDuration;
frameNow = animationStep;
} else {
currentTime = avAudioPlayer.currentTime;
frameNow = (NSInteger) (currentTime / animationFrameDuration);
}
// Limit the range of frameNow to [0, SIZE-1]
if (frameNow == 0) {
frameNow = 0;
} else if (frameNow >= animationNumFrames) {
frameNow = animationNumFrames - 1;
}
[self animationShowFrame: frameNow];
// animationStep = frameNow + 1;
if (animationStep >= animationNumFrames) {
[self stopAnimating];
// Continue to loop animation until loop counter reaches 0
if (animationRepeatCount > 0) {
self.animationRepeatCount = animationRepeatCount - 1;
[self startAnimating];
}
}
}
// Display the given animation frame, in the range [1 to N]
// where N is the largest frame number.
- (void) animationShowFrame: (NSInteger) frame {
if ((frame >= animationNumFrames) || (frame < 0))
return;
NSData *data = [animationData objectAtIndex:frame];
UIImage *img = [UIImage imageWithData:data];
imageView.image = img;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.animationRepeatCount = 0;
[self stopAnimating];
}
@end
//
//ImageAnimatorViewController.m
//PNGAnimatorDemo
//
//由摩西·德容于2009年2月5日创作。
//
#导入“ImageAnimatorViewController.h”
#进口
#进口
@ImageAnimatorViewController的实现
@合成AnimationURL、animationFrameDuration、animationNumFrames、animationRepeatCount、,
imageView、animationData、animationTimer、animationStep、animationDuration、animationOrientation、viewCGRect;
@合成animationAudioURL、avAudioPlayer;
-(无效)解除锁定{
//设置动画时无法释放此对象,这可能会
//仅当用户代码错误地删除了最后一个引用时才会发生。
NSAssert([自isAnimating]==FALSE,@“在仍设置动画时解除锁定”);
self.animationURL=nil;
self.imageView=nil;
self.animationData=nil;
self.animationTimer=nil;
[super dealoc];
}
+(ImageAnimatorViewController*)ImageAnimatorViewController
{
返回[[[ImageAnimatorViewController alloc]初始化]自动释放];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
//实现loadView以编程方式创建视图层次结构,而不使用nib。
-(void)负荷视图{
UIView*myView=[[UIView alloc]initWithFrame:viewCGRect];
[myView自动释放];
self.view=myView;
/*UIView*myView=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
[myView自动释放];
self.view=myView;
//FIXME:附加支持方向
if(animationOrientation==UIImageOrientationUp){
//无操作
}else if(animationOrientation==UIImageOrientationLeft){
//逆时针90度
//[自转景观];
}else if(animationOrientation==UIImageOrientationRight){
//连续90度
//[自转景观权];
}否则{
NSAssert(错误,@“不支持的动画定向”);
}
*/
//前景动画图像
UIImageView*myImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
[myImageView自动释放];
self.imageView=myImageView;
//动画数据应该已经加载到内存中,因为
//设置animationURLs属性
NSAssert(animationURLs,@“未定义animationURLs”);
NSAssert([AnimationURL计数]>1,@“AnimationURL必须至少包含2个URL”);
NSAssert(animationFrameDuration,@“animationFrameDuration未定义”);
/