Iphone 如何在lybrary中旋转和调整类精灵照片的大小
我如何在ccTouchesMoved中旋转sprite在cocos2d中的中心。我找到了一些代码,但这不是我需要的。我需要在照片库应用程序中旋转类似精灵的照片Iphone 如何在lybrary中旋转和调整类精灵照片的大小,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,我如何在ccTouchesMoved中旋转sprite在cocos2d中的中心。我找到了一些代码,但这不是我需要的。我需要在照片库应用程序中旋转类似精灵的照片 - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *allTouch = [touches allObjects]; if([allTouch count] > 1){ UITouch *touch =
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouch = [touches allObjects];
if([allTouch count] > 1){
UITouch *touch = [allTouch objectAtIndex:0];
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL:point];
float angleRadians = atanf((float)point.y / (float)point.x);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
imageFromPicker.rotation = imageFromPicker.rotation + cocosAngle;
}
}
也许这不是最好的方法,但我用这个: 当我用两个手指触摸精灵时,我使用两点的圆弧旋转精灵: 首先,我使用此函数查找方向:
static inline int ccw(CGPoint p0, CGPoint p1, CGPoint p2)
{
int dx1, dx2, dy1, dy2;
dx1 = p1.x - p0.x; dy1 = p1.y - p0.y;
dx2 = p2.x - p0.x; dy2 = p2.y - p0.y;
int v1 = dx1 * dy2;
int v2 = dy1 * dx2;
if (v1 > v2)
return 1;
if (v1 < v2)
return -1;
if ((dx1*dx2 < 0) || (dy1*dy2 < 0))
return -1;
if ((dx1*dx1 + dy1*dy1) < (dx2*dx2 + dy2*dy2))
return 1;
return 0;
}
静态内联int ccw(CGPoint p0、CGPoint p1、CGPoint p2)
{
int dx1,dx2,dy1,dy2;
dx1=p1.x-p0.x;dy1=p1.y-p0.y;
dx2=p2.x-p0.x;dy2=p2.y-p0.y;
int v1=dx1*dy2;
int v2=dy1*dx2;
如果(v1>v2)
返回1;
如果(v1
在使用CCTouchsMoved方法后,我会这样做:
if ([allTouches count] == 2) {
rotating=TRUE;
NSArray *twoTouches = [allTouches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
CGPoint a = [first previousLocationInView:[touch view]];
CGPoint b = [second previousLocationInView:[touch view]];
CGPoint c = [first locationInView:[touch view]];
int direction =ccw(b, a, c);
float pX= fabs(c.x-b.x);
float pY= fabs(c.y-b.y);
float rotation = atan2(pY, pX);
// rotTotal= (rotTotal+rotacion)/10;
if(direction<0)
YourSprite.rotation=(YourSprite.rotation-rotation);
else
YourSprite.rotation=(YourSprite.rotation+rotation);
}
if([AllTouchs count]==2){
旋转=真;
NSArray*TwoTouchs=[AllTouchs AllObject];
UITouch*first=[TwoTouchs对象索引:0];
UITouch*second=[TwoTouchs对象索引:1];
CGA点=[first previousLocationInView:[touch view]];
CGB点=[second previousLocationInView:[touch view]];
CGPoint c=[第一个位置视图:[触摸视图]];
内部方向=逆时针方向(b、a、c);
浮动pX=fabs(c.x-b.x);
浮动pY=晶圆厂(c.y-b.y);
浮动旋转=atan2(pY,pX);
//rototal=(rototal+rotacion)/10;
如果(方向如果你想更平滑地旋转,你可以在这里添加一个系数float rotation=atan2(pY,pX)*coef;它可以工作:)我添加了一个系数,它看起来非常好。我还添加了缩放方法,剩下的就是添加遮罩,然后保存精灵。谢谢!