Pages

Subscribe:

Tuesday, February 8, 2011

Using image & text together or custom view in a UIPickerView

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(5,5, 50, 50);
UIImage *anImage=[UIImage imageNamed:yourImage];
[customButton setImage:anImage forState:UIControlStateNormal];

UILabel *lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 400, 50)];
lblTemp.text = [NSString stringWithFormat:yourText];

UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 460, 60)];
[tempView addSubview:lblTemp];
[tempView addSubview:customButton];
[view addSubview:tempView];

[lblTemp release];
[tempView release];

return tempView;
}

Wednesday, February 2, 2011

Scaling & Rotating UIImageView through 2 finger swipe

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSArray *allTouches = [touches allObjects];

UITouch* t;
if([[event allTouches] count]==1){
if (CGRectContainsPoint([smallImage frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {
t=[[[event allTouches] allObjects] objectAtIndex:0];
touch1=[t locationInView:nil];
}
}else{
t=[[[event allTouches] allObjects] objectAtIndex:0];
touch1=[t locationInView:nil];
t=[[[event allTouches] allObjects] objectAtIndex:1];
touch2=[t locationInView:nil];
}
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CGPoint currentTouch1;
CGPoint currentTouch2;
NSArray *allTouches = [touches allObjects];
UITouch* t;
float scale,rotation;

if([[event allTouches] count]==1){
t=[[[event allTouches] allObjects] objectAtIndex:0];
if (CGRectContainsPoint([smallImage frame], [[allTouches objectAtIndex:0] locationInView:self.view]))
{
touch2=[t locationInView:nil];
smallImage.center=CGPointMake(smallImage.center.x+touch2.x-touch1.x,smallImage.center.y+touch2.y-touch1.y);
touch1=touch2;
}
}
else if([[event allTouches] count]==2)
{
t=[[[event allTouches] allObjects] objectAtIndex:0];
currentTouch1=[t locationInView:nil];

t=[[[event allTouches] allObjects] objectAtIndex:1];
currentTouch2=[t locationInView:nil];


scale = [self distance:currentTouch1 toPoint:currentTouch2] / [self distance:touch1 toPoint:touch2];
rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
if(isnan(scale)){
scale=1.0f;
}
NSLog(@"rotation %f",rotation);

NSLog(@"scale %f",scale);

if (CGRectContainsPoint([smallImage frame], [[allTouches objectAtIndex:0] locationInView:self.view]) &&
CGRectContainsPoint([smallImage frame], [[allTouches objectAtIndex:1] locationInView:self.view]))
{

smallImage.transform=CGAffineTransformScale(smallImage.transform, scale,scale);
smallImage.transform=CGAffineTransformRotate(smallImage.transform, rotation);
}
else // In case of scaling or rotating the background imageView
{
imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
}

touch1=currentTouch1;
touch2=currentTouch2;
}
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
return sqrt(fabs(point1.x - point2.x) + fabs(point1.y - point2.y));
}