2010-02-04 5 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 란 무엇입니까? –

+0

다른 사용자 정의 클래스. 앱을 제거 할 때 스프링 보드와 같은 흔들림 효과를 만드는 것이 었습니다. 그것은 당신의 문제와 관련이 없습니다. rotateRight 및 rotateLeft 메서드와 자체 터치 메서드를 살펴보고 싶습니다. 이러한 메서드는 뷰의 시각적 표시를 회전시키고 뷰가 표시하는 실제 이미지에는 영향을 미치지 않습니다. 즉, 뷰를 저장하거나 다른 뷰에 합성하면 이미지의 원래 방향을 얻게됩니다. – TechZen

+0

이제 괜찮아요. 볼을 만들어서 화면의 맨 위에서 바닥으로 보냅니다. 이걸로 3 개의 이미지 (3 개의 볼과 같은 이미지)를 찍었습니다. 이제 무작위로 볼을 선택할 수 있습니다. 그래서 어떻게 볼을 찾을 수 있습니까? 교차 했나요? –

0

컨트롤러에 드래그 시작을 추적 할 수있는 ivar가있을 수 있습니다. '터치 다운'이벤트가 발생하면 시작 지점을 저장하십시오. '터치 이동'이벤트가 발생하면 현재 지점에서 저장된 지점까지의 거리를 계산하십시오. 둘 사이의 유클리드 거리 (Euclidean distance)는 회전 (회전 방향을 알려주는 부호)을 줄 수 있습니다. 회전을 적절하게 조절합니다 (예 :보기 너비의 절반이 180 °와 같습니다). 이미지의 변환 회전을 설정합니다.

+0

샘플을 게시 할 수 있습니까? –

+0

여기에는 기존 코드가 없습니다. 방금 문제를 어떻게 해결할 지 설명했습니다. 위의 설명을 너무 어려움없이 코드로 변환 할 수 있어야합니다! 재밌게 지내세요 ... – gavinb

+0

괜찮아요. 반쯤 있지만 문제가 있습니다. ImageView는 회전하면서 지그재그로 바뀌고 있습니다. 어떻게 해결할 수 있습니까? 또한 단지 왼쪽이나 오른쪽으로 회전 할 때만 시작점에서부터 왼쪽이나 오른쪽으로 움직입니다. 나는 Imageview를 왼쪽이나 오른쪽으로 움직일 때 항상 회전 시키길 원합니다. 위의 링크를 보았습니다. 정확하게 그 모양을 원해주세요. 올바른 방향으로보고 가이드하십시오. –

관련 문제