Search This Blog

Thursday, November 28, 2013

Touches Sample in iPhone SDK


Touches

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAlertViewDelegate,UIActionSheetDelegate>
{
  
}

-(IBAction)cleanAll:(id)sender;
@end


.m

#import "ViewController.h"

@implementation ViewController


-(IBAction)cleanAll:(id)sender;
{
    NSArray *subViewsArray = [self.view subviews];
    for (int i=0; i<[subViewsArray count]; i++)
    {
        UIView *vi = [subViewsArray objectAtIndex:i];
        if(![vi isKindOfClass:[UIButton class]])
           [vi removeFromSuperview];
    }


}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan called");
   
    UITouch *touch = [touches  anyObject];
    CGPoint pnt = [touch locationInView:self.view];
    NSLog(@"touch point is (%0.0f,%0.0f)",pnt.x,pnt.y);
    
    UILabel *lbl = [[UILabel allocinitWithFrame:CGRectMake(pnt.x, pnt.y33)];
    [lbl setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:lbl];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
      NSLog(@"touchesMoved called");
    UITouch *touch = [touches  anyObject];
    CGPoint pnt = [touch locationInView:self.view];
    NSLog(@"touch point is (%0.0f,%0.0f)",pnt.x,pnt.y);
    
    UILabel *lbl = [[UILabel allocinitWithFrame:CGRectMake(pnt.x, pnt.y33)];
    [lbl setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:lbl];


}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   NSLog(@"touchesEnded called");
}

@end

No comments:

Post a Comment