我是Objective-C的新手,我的应用程序遇到了一些问题。 我将UIView子类化并覆盖drawRect:方法以绘制三角形。 然后我厌倦了通过旋转三角形并在屏幕上移动三角形来制作动画。 为此,我使用了NSTimer类并将其设置为每1/100秒触发一次。 我的应用程序中有更多NSTimers,我用它们来制作视图并计算得分 但是我注意到NSTimer在短时间内不是很准确,比如1/100秒和 特别是当你有一个以上时。
我想问是否有更好的方法来做我想做的事情?如果有更准确的计时器用于此类任务?或者定时器不是最好的方法吗?
这是spinningTriangle.h:
#import <UIKit/UIKit.h>
@interface SPDSpinningTriangleView : UIView
{
int changeInDegrees ;
CGPoint triangleCenter ;
UISwipeGestureRecognizer *swipe ;
float score ;
}
@end
这里是spinningTriangle.m:
@implementation SPDSpinningTriangleView
const float MAX_SCORE = 10.0 ;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]] ;
CGRect triangleFrame = CGRectMake(0, 0, 80, 80) ;
[self setFrame:triangleFrame] ;
changeInDegrees = 0 ;
score = 0.0 ;
[NSTimer scheduledTimerWithTimeInterval:1/100.0 target:self selector:@selector(updateView:) userInfo:nil repeats:YES] ;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
const float HALF_FRAME = rect.size.height / 2.0 ;
if(changeInDegrees == 0)
{
swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipped:)] ;
[[self superview] addGestureRecognizer:swipe] ;
UIView *superView = [self superview] ;
float randomY = arc4random() % (int)[superView frame].size.height + HALF_FRAME;
while (randomY > CGRectGetMaxY([superView frame]) - HALF_FRAME) {
randomY = arc4random() % (int)[superView frame].size.height + HALF_FRAME;
}
triangleCenter = CGPointMake(CGRectGetMinX([superView frame]),randomY) ;
[self setCenter:triangleCenter] ;
} else {
triangleCenter.x += 2 ;
[self setCenter:triangleCenter] ;
}
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext() ;
CGContextTranslateCTM(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect)) ;
CGContextRotateCTM(ctx,changeInDegrees * M_PI / 180.0) ;
CGContextTranslateCTM(ctx, -CGRectGetMidX(rect), -CGRectGetMidY(rect)) ;
CGContextBeginPath(ctx) ;
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)) ;
CGPoint vertex1 = CGPointMake(center.x, center.y - HALF_FRAME) ;
CGPoint vertex2 = CGPointMake(center.x - (HALF_FRAME * 1.5 * tanf(30.0 * M_PI / 180.0)), vertex1.y + HALF_FRAME * 1.5) ;
CGPoint vertex3 = CGPointMake(center.x + (HALF_FRAME * 1.5 * tanf(30.0 * M_PI / 180.0)), vertex2.y) ;
CGContextMoveToPoint(ctx, vertex1.x, vertex1.y) ;
CGContextAddLineToPoint(ctx,vertex2.x, vertex2.y) ;
CGContextAddLineToPoint(ctx, vertex3.x, vertex3.y) ;
CGContextClosePath(ctx) ;
CGContextSetRGBFillColor(ctx, 0.5, 0.0, 1.0, 1.0) ;
CGContextFillPath(ctx) ;
}
-(void) swipped:(UIGestureRecognizer *) gesture
{
NSLog(@"swipe") ;
[self clearsContextBeforeDrawing] ;
[[self superview] removeGestureRecognizer:gesture] ;
[self removeFromSuperview] ;
}
-(void) dealloc
{
NSLog(@"Spinning triangle destroyed") ;
[[SPDScoreManager scoreManger] addToScore:score] ;
}
-(void) updateView: (NSTimer *) t
{
if (triangleCenter.x < CGRectGetMaxX([[self superview] frame]))
{
[self setNeedsDisplay] ;
changeInDegrees += 5 ;
score = MAX_SCORE - (triangleCenter.x / [self frame].size.width) * MAX_SCORE ;
}
else
{
NSLog(@"Faild") ;
[t invalidate] ;
[[SPDScoreManager scoreManger] addToFails] ;
[[self superview] removeGestureRecognizer:swipe] ;
[self removeFromSuperview] ;
}
}
@end