1,实现特效的两种用法:For example, the ViewTransitions sample project from apple uses code like:(1)CATransition *applicationLoadViewIn = [CATransition animation];[applicationLoadViewIn setDuration:1];[applicationLoadViewIn setType:kCATransitionReveal];[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];but there are also code snippets floating around the net that look like this:(2)[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:0.75];[UIView setAnimationDelegate:self];[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];[myview removeFromSuperview];[UIView commitAnimations];The difference seems to be the amount of control you need over the animation.The CATransition approach gives you more control and therefore more things to set up, eg. the timing function. Being an object, you can store it for later, refactor to point all your animations at it to reduce duplicated code, etc.The UIView class methods are convenience methods for common animations, but are more limited than CATransition. For example, there are only four possible transition types (flip left, flip right, curl up, curl down). If you wanted to do a fade in, you'd have to either dig down to CATransition's fade transition, or set up an explicit animation of your UIView's alpha.Note that CATransition on Mac OS X will let you specify an arbitrary CoreImage filter to use as a transition, but as it stands now you can't do this on the iPhone, which lacks CoreImage.I have been using the latter for a lot of nice lightweight animations. You can use it crossfade two views, or fade one in in front of another, or fade it out. You can shoot a view over another like a banner, you can make a view stretch or shrink... I'm getting a lot of mileage out of beginAnimation/commitAnimations.Don't think that all you can do is:[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];Here is a sample:[UIView beginAnimations:nil context:NULL]; { [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; [UIView setAnimationDelegate:self]; if (movingViewIn) {// after the animation is over, call afterAnimationProceedWithGame// to start the game [UIView setAnimationDidStopSelector:@selector(afterAnimationProceedWithGame)];// [UIView setAnimationRepeatCount:5.0]; // don't forget you can repeat an animation// [UIView setAnimationDelay:0.50];// [UIView setAnimationRepeatAutoreverses:YES]; gameView.alpha = 1.0; topGameView.alpha = 1.0; viewrect1.origin.y = selfrect.size.height - (viewrect1.size.height); viewrect2.origin.y = -20; topGameView.alpha = 1.0; } else { // call putBackStatusBar after animation to restore the state after this animation [UIView setAnimationDidStopSelector:@selector(putBackStatusBar)]; gameView.alpha = 0.0; topGameView.alpha = 0.0; } [gameView setFrame:viewrect1]; [topGameView setFrame:viewrect2];} [UIView commitAnimations];As you can see, you can play with alpha, frames, and even sizes of a view. Play around. You may be surprised with its capabilities2。捕捉touch事件:下面是打印的一些有用信息---下面是UICView的声明---You must customize UIView, and handle event.//UICView.h@interface UICView : UIView@end---下面是UICView的定义---//UICView.m@implementation UICView//Handle touches began event- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { printf("members count=%i\r\n", touches.count); for (UITouch *touch in touches) { printf("tap count = %i\r\n", touch.tapCount); //for dragging event, touch.tapCount is 0 } [self touchesBegin:touches withEvent:event]; //You must invoke this interface, otherwise, application may can't handle event correctly.}@end下面是相关事件:iphone开发-UIView(touch事件)iphone 2009-03-14 08:35:04 阅读130 评论0 字号:大中小怎样捕获手指的触摸事件呢?理论不说了,在开发文档里面说的很详细。就说说实现。其实就是要重写实现三个函数,如下:touch开始时会调用touchesBegan- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSSet *allTouches = [event allTouches]; UITouch *touch = [touches anyObject]; NSLog(@"touch view++++++%@", [touch view]); // [touch view]获得当前touch的view //[allTouches count]获得当前touch的是否是多触摸,如果 [allTouches count] == 1就不是多触摸。 switch ([allTouches count]) { case 1: { // potential pan gesture UITouch *touch = [[allTouches allObjects] objectAtIndex:0]; [self setPanningModeWithLocation:[touch locationInView:self]]; } break; case 2: { // potential zoom gesture UITouch *touch0 = [[allTouches allObjects] objectAtIndex:0]; UITouch *touch1 = [[allTouches allObjects] objectAtIndex:1]; CGFloat spacing = [self eucledianDistanceFromPoint:[touch0 locationInView:self] toPoint:[touch1 locationInView:self]]; [self setZoomingModeWithSpacing:spacing]; } break; default: [self resetTouches]; break; } } //是指触摸移动时,调touchesMoved#define ZOOM_IN_TOUCH_SPACING_RATIO (0.75)#define ZOOM_OUT_TOUCH_SPACING_RATIO (1.5)- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ //获取当前touch的点 CGPoint location = [touch locationInView:self];switch ([allTouches count]) { case 1: { } break; case 2: { UITouch *touch0 = [[allTouches allObjects] objectAtIndex:0]; UITouch *touch1 = [[allTouches allObjects] objectAtIndex:1]; CGFloat spacing = [self eucledianDistanceFromPoint:[touch0 locationInView:self] toPoint:[touch1 locationInView:self]]; CGFloat spacingRatio = spacing / lastTouchSpacing_; if (spacingRatio >= ZOOM_OUT_TOUCH_SPACING_RATIO){ [self zoomIn]; } else if (spacingRatio
1 楼 lukejin 2010-03-22
这写的对读者也太不友好了吧,哈哈,。