2013-11-16 2 views
1

Я вращающаяся ImageView, который Минуит рука часы сПоворот ImageView в соответствии с прикосновением

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    UITouch *touch = [touches anyObject]; 
    if ([touch view] == _imgViewMinuit) 
    { 
     NSLog(@"Touched"); 
     _imgMinuitHand.transform = CGAffineTransformRotate(_imgMinuitHand.transform,DEGREES_RADIANS(6)); 
    } 
} 

Теперь я хочу, чтобы повернуть его, как на мой палец, по часовой стрелке/против часовой стрелки со скоростью моего прикосновения.

Как я могу это достичь?

ответ

2

проверить этот код

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch=[[event allTouches]anyObject]; 

    [self transformSpinnerwithTouches:touch]; 
} 

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation 
{ 
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view]; 
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view]; 
    //origin is the respective piont from that i gonna measure the angle of the current position with respective to previous position .... 
    CGPoint origin; 
    origin.x = arrow.center.x; 
    origin.y = arrow.center.y; 
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint]; 
    CGAffineTransform newTransform = CGAffineTransformScale(arrow.transform, 1, 1); 
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x); 
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint]; 
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x); 
    CGFloat newAngle = currentRotation- previousRotation; 
    temp1 = temp1 + newAngle; 
    //NSLog(@"temp1 is %f",temp1); 
    //NSLog(@"Angle:%F\n",(temp1*180)/M_PI); 
    newTransform = CGAffineTransformRotate(newTransform, newAngle); 
    [self animateView:arrow toPosition:newTransform]; 
} 
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform 
{ 
arrow.transform = newTransform; 
} 

-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint 
{ 
    CGFloat x = secondPoint.x - firstPoint.x; 
    CGFloat y = secondPoint.y - firstPoint.y; 
    CGPoint result = CGPointMake(x, y); 
    //NSLog(@"%f %f",x,y); 
    return result; 
} 
+0

@SourabhKumbhar ли он работал ?? – Vizllx

+1

Спасибо ... очень ... его работа. всего 2 вопроса. 1. Поскольку у меня есть две руки для часов, вам нужно вращать их, когда пользователь прикасается к ним и тащит их. 2. Могу ли я поворачивать их с фиксированным углом 6 градусов или 30 градусов? Кроме того, я рассчитываю время, основанное на угле. и часовая стрелка должна двигаться только с разницей в часах в часах. – sourabhkumbhar

+0

вы только перемещаете вращение на 6 градусов каждый раз, вам нужно рассчитать угол, на котором должна быть рука, в зависимости от положения касания говорят, что у вас есть точка касания x, y относительно начала координат в центре часы, вы можете рассчитать угол с тригонометрией CGFloat angle = atan2 (y, x); _imgHand.transform = CGAffineTransformMakeRotation (угол); – Vizllx