2010-02-04 294 views

回答

1

我去年擺弄這個。事實證明,這比我想象的要複雜得多。 IIRC,這個小班做了你想要的。這是一個UIButtonSubclass,它顯示圖像並響應點擊和拖動。請注意,這只是臨時代碼。它不做任何內存管理,清理等。

#import <UIKit/UIKit.h> 
#import "BackgroundImageButton.h" 
#import "WiggleImageView.h" 

@interface StickerButton : UIButton { 
    //ivars used to control selection animaiton 
    CGAffineTransform startTransform; 
    BOOL currentlyAnimating; 
    BOOL shouldAnimate; 
    //ivars to handle touches and control events 
    BOOL wasDrag; 
    BOOL wasTouchDown; 
    WiggleImageView * imgView; 
} 
//ivars used to control selection animaiton 
@property CGAffineTransform startTransform; 
@property(nonatomic, retain) WiggleImageView *imgView; 
@property BOOL currentlyAnimating; 
@property BOOL shouldAnimate; 
//ivars to handle touches and control events 
@property BOOL wasDrag; 
@property BOOL wasTouchDown; 


#pragma mark Initialization 
-(id) initWithImage:(UIImage *)anImage atCenterPoint:(CGPoint) centerPoint; 

#pragma mark Selection Animation Methods 
-(void) animateSelection; 
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context; 

#pragma mark Self Touch Methods //not as dirty as it sounds. 
-(void) touchDragSelf:(id)sender withEvent:(UIEvent *) theEvent; 
-(void) touchDownSelf:(id)sender withEvent:(UIEvent *) theEvent; 
-(void) touchUpSelf:(id)sender withEvent:(UIEvent *) theEvent; 

#pragma mark Graphic Edit Methods 
-(void) rotateRight; 
-(void) rotateLeft; 
-(void) scaleUp; 
-(void) scaleDown; 
-(void) select; 
-(void) deselect; 
-(void) translateMoveByX:(CGFloat) dx andY:(CGFloat) dy; //used by self to account for translated coordinates 
-(void) frameMoveByX:(CGFloat) dx andY:(CGFloat) dy; //used by external to move frame in superview  
@end 

#import "StickerButton.h" 

@implementation StickerButton 
@synthesize startTransform; 
@synthesize currentlyAnimating; 
@synthesize shouldAnimate; 
@synthesize wasDrag; 
@synthesize wasTouchDown; 
@synthesize imgView; 


#pragma mark Initialization 
- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     startTransform=self.transform; 
     currentlyAnimating=NO; 
     shouldAnimate=NO; 
     wasDrag=NO; 
     wasTouchDown=NO; 
     self.adjustsImageWhenDisabled=NO; 
     self.adjustsImageWhenHighlighted=NO; 
     self.showsTouchWhenHighlighted=NO; 
     [self addTarget:self action:@selector(touchDownSelf:withEvent:) forControlEvents:UIControlEventTouchDown]; 
     [self addTarget:self action:@selector(touchDragSelf:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
     [self addTarget:self action:@selector(touchUpSelf:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return self; 
} 

-(id) initWithImage:(UIImage *)anImage atCenterPoint:(CGPoint) centerPoint{ 
    CGFloat xOrigin,yOrigin; 
    xOrigin=centerPoint.x-(anImage.size.width/2); 
    yOrigin=centerPoint.y-(anImage.size.height/2); 
    [self initWithFrame:CGRectMake(xOrigin, yOrigin, anImage.size.width, anImage.size.height)]; 
    WiggleImageView *w=[[WiggleImageView alloc] initWithFrame:self.bounds]; 
    imgView=w; 
    imgView.image=anImage; 
    [self addSubview:imgView]; 
    return self; 
}//------------------------------------initWithImage:atCenterPoint:------------------------------------ 



#pragma mark Selection Animation Methods 
-(void) animateSelectedThrob{ 
    if (!currentlyAnimating) { 
     NSLog(@"animating"); 
     currentlyAnimating=YES; 
     //startTransform=self.transform; //this has to be saved to prevent some kind of rounding error from gradually rotating the view 
     [UIView beginAnimations:@"selectionAnimation" context:nil]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.1]; 
     [UIView setAnimationRepeatCount:1]; 
     [UIView setAnimationRepeatAutoreverses:YES]; 
     self.transform=CGAffineTransformScale(self.transform, 1.1, 1.1); 
     [UIView commitAnimations]; 
    } 
}//-------------------------------------(void) animateSelectedThrob------------------------------------ 


-(void) animateSelection{ 
    if (!currentlyAnimating) { 
     //NSLog(@"animating"); 
     currentlyAnimating=YES; 
     startTransform=self.transform; //this has to be saved to prevent some kind of rounding error from gradually rotating the view 
     [UIView beginAnimations:@"selectionAnimation" context:nil]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.1]; 
     [UIView setAnimationRepeatCount:2]; 
     [UIView setAnimationRepeatAutoreverses:YES]; 
     self.transform=CGAffineTransformRotate(self.transform, (2 * M_PI/180)); 
     [UIView commitAnimations]; 
    } 

}//-------------------------------------(void) animateSelection------------------------------------ 

-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{ 
    self.transform=startTransform; 
    currentlyAnimating=NO; 
    if (shouldAnimate) { 

     [self animateSelection]; 
    } 
}//------------------------------------animationDidStop:finished:context:------------------------------------ 

#pragma mark Self Touch Methods 

-(void) touchDownSelf:(id)sender withEvent:(UIEvent *) theEvent{ 
    NSLog(@"touchDownSelf"); 
    wasTouchDown=YES; 
    shouldAnimate=NO; 
}//------------------------------------touchDownSelf:withEvent:------------------------------------ 

-(void) touchDragSelf:(id)sender withEvent:(UIEvent *) theEvent{ 
    NSLog(@"touchDragSelf"); 
    if ([[theEvent touchesForView:self] count]==1) { 
     UITouch *t=[[theEvent touchesForView:self] anyObject]; 
     CGPoint l,p; 
     l=[t locationInView:self]; 
     p=[t previousLocationInView:self]; 
     [self translateMoveByX:(l.x-p.x) andY:(l.y-p.y)]; 
     wasDrag=YES;   
    } 

}//------------------------------------touchDragSelf:withEvent:------------------------------------ 

-(void) touchUpSelf:(id)sender withEvent:(UIEvent *) theEvent{ 
    NSLog(@"touchUpSelf"); 
// if (!wasDrag && wasTouchDown) { 
//  [self select]; 
// } 
    if (wasTouchDown) { 
     [self select]; 
    } 
    wasDrag=NO; 
    wasTouchDown=NO; 
}//------------------------------------touchUpSelf:withEvent:------------------------------------ 

#pragma mark Graphic Edit Methods 
-(void) rotateRight{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.1]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.transform=CGAffineTransformRotate(self.transform, (M_PI/180)); 
    [UIView commitAnimations]; 
}//-------------------------------------(void) rotateRight------------------------------------ 

-(void) rotateLeft{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.1]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.transform=CGAffineTransformRotate(self.transform, (-1*M_PI/180)); 
    [UIView commitAnimations]; 
}//-------------------------------------(void) rotateLeft------------------------------------ 

-(void) scaleUp{ 
    //todo add variable to track total scale so it doesn't get to big and cause problems 
    shouldAnimate=NO; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.1]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.transform=CGAffineTransformScale(self.transform, 1.1, 1.1); 
    [UIView commitAnimations]; 
    startTransform=self.transform; 
}//-------------------------------------(void) scaleUp------------------------------------ 

-(void) scaleDown{ 
    //todo add variable to track total scale so it doesn't get to small and cause problems 
    shouldAnimate=NO; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.1]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.transform=CGAffineTransformScale(self.transform, 0.9, 0.9); 
    [UIView commitAnimations]; 
    startTransform=self.transform; 
}//-------------------------------------(void) scaleDown------------------------------------ 

-(void) select{ 
    [self animateSelectedThrob]; 
    imgView.shouldWiggle=YES; 
} 
//-------------------------------------(void) select------------------------------------ 

-(void) deselect{ 
    imgView.shouldWiggle=NO; 
} 

-(void) translateMoveByX:(CGFloat) dx andY:(CGFloat) dy{ //necessary for all points that orignate within the transformed view 
    NSLog(@"dx=%f,dy=%f",dx,dy); 
    shouldAnimate=NO; 
    self.transform=CGAffineTransformTranslate(self.transform, dx,dy); 
    startTransform=self.transform; 
}//------------------------------------translateMoveByX:andY:------------------------------------ 

-(void) frameMoveByX:(CGFloat) dx andY:(CGFloat) dy{ //necessary for all points that originate outside the transformed view 

    self.frame=CGRectMake(self.frame.origin.x+dx, self.frame.origin.y+dy, self.frame.size.width, self.frame.size.height); 
}//------------------------------------frameMoveByX:andY:------------------------------------ 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+1

什麼是WiggleImageView? – 2010-02-05 05:16:57

+0

另一個自定義類。這是爲了在您移除應用程序時在跳板上創建擺動效果。這與你的問題無關。您想查看rotateRight和rotateLeft方法以及自觸摸方法。請注意,這些方法只是旋轉視圖的視覺顯示,不會影響視圖顯示的實際圖像,即如果您保存視圖或將其合成到另一個視圖上,則會獲得圖像的原始方向。 – TechZen 2010-02-05 12:39:51

+0

好吧,現在告訴我,我正在創建一個球,並將其從屏幕頂部發送到底部。爲此,我已經拍攝了3張圖像(如3個球),現在我在創建時隨機選擇球,那麼如何檢測哪個球被相交? – 2010-02-05 13:24:28

0

你可以在你的控制器中有一個ivar來跟蹤拖動的開始。當您遇到「觸及」事件時,請保存起點。當您觸摸移動的事件時,計算從當前點到保存點的距離。兩者之間的歐幾里得距離可以給你旋轉(標誌告訴你旋轉的方向)。適當地縮放旋轉角度(即可能是視圖寬度的一半相當於180度),並設置圖像變換的旋轉。

+0

你可以發佈一些示例嗎? – 2010-02-05 05:15:12

+0

我沒有任何現有的代碼。我剛纔解釋了我將如何開始解決這個問題。你應該能夠將上面的描述轉換成代碼而沒有太多困難!玩得開心... – gavinb 2010-02-05 12:04:57

+0

好吧,我得到了一半,但它的問題是ImageView旋轉時得到ZigZagged。如何解決這個問題?它也只是從開始點向左或向右旋轉。我的意思是我只是想讓它總是在我將Im​​ageview向左或向右移動的時候旋轉。同樣來自您的一個問題,您是否看到了上述鏈接?想要它就像這樣,請看看並指導正確的方向 – 2010-02-11 05:11:24