UILabel not responding to touch event - ios

I am using following code to create my view. My UILabel is inside a scrollview. now i want click event in touchesBegan method of my custom label. but unable to get it.
img1 = [[customLabel alloc] initWithFrame:CGRectMake(0,height ,280,10 )];//
[img1 setText: [self.answer_set objectAtIndex:i]];
[img1 setTextAlignment:NSTextAlignmentLeft];
[img1 setBackgroundColor:[UIColor colorWithRed:127.0f/255.0f green:165.0f/255.0f blue:234.0f/255.0f alpha:1.0f]];
img1.textColor = [UIColor whiteColor];
img1.lineBreakMode = NSLineBreakByWordWrapping;
img1.numberOfLines = 0;
[img1 sizeToFit];
img1.contentMode = UIViewContentModeScaleToFill;
img1.font = [UIFont fontWithName:#"Arial" size:14.0f] ;
CGFloat labelHeight=[self getStringHeightforLabel:img1];
NSLog(#"LABEL HEIGHT:: %f",labelHeight);
//set frame after getting height
if (labelHeight < 60.0) {
labelHeight = 60.0;
}
[img1 setUserInteractionEnabled:YES];
img1.tag = 5000;
img1.frame=CGRectMake(-5,height,285 + 10,labelHeight + 10);
img1.layer.cornerRadius = 5;
/** Shadow */
CALayer *shadowLayer = [[CALayer alloc] init];
shadowLayer.frame = CGRectMake(-5,height ,img1.frame.size.width,img1.frame.size.height);
shadowLayer.cornerRadius = 10;
shadowLayer.backgroundColor = [UIColor blueColor].CGColor;
shadowLayer.shadowColor = [UIColor colorWithRed:127.0f/255.0f green:165.0f/255.0f blue:234.0f/255.0f alpha:1.0f].CGColor;
shadowLayer.shadowOpacity = 0.6;
shadowLayer.shadowOffset = CGSizeMake(0,0);
shadowLayer.shadowRadius = 5;
[img1.layer setMasksToBounds:YES];
[shadowLayer addSublayer:img1.layer];
[self.ans_labels.layer addSublayer:shadowLayer];
height += img1.frame.size.height + 15;
and to get event:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
NSLog(#"worked",nil);
//[self giveAnswer:touch.view.tag];
if(touch.view.tag == 5000){
// do here
}
}
I was able to get event earlier,but not today. what i'm doing wrong ???

yourLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTap)];
tapGesture.delegate=self;
[yourLabel addGestureRecognizer:tapGesture];
- (void)labelTap:(UITapGestureRecognizer *)recognize {
}

Finally I ended up with following solution :
1) I added following code to my UIScrollView:
scroll.userInteractionEnabled = YES;
scroll.exclusiveTouch = YES;
scroll.canCancelContentTouches = YES;
scroll.delaysContentTouches = YES;
2) I wanted a touch event, So I added a UIButton over my UILabel of type custom,with clear background color and having same frame size as of label.
3) and last and most important thing, I must have to add UIButton to UIScrollView.
All these give me exact functionality which I wanted.

Try like this
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
}

Related

UIControll in UIScrollView not receiving touch events

I use SevenSwitch in my project. I need to add it into a UIScrollView but it seems that the control can not receive touch events when I add it into scroll view.
I tried sub classing scrollview and overriding below code:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
also added:
self.scrollView.delaysContentTouches = NO;
but still can not receive the touch event. How can I stop scrollview from preventing the UIControl from receiving touches?
Update
I have a tap gesture on my scroll view because I want that when user tap the scroll view I call [self.scrollView endEditing:YES] to close the keyboard. When I remove it the seven switch is working with tap.
I add below code to my tap gesture:
tap.cancelsTouchesInView = NO;
and now the sevenswitch is working with tap but there is problems when make the switch on or off with touch tracking.
I have made sample in which i have sevenswitch inside the scrollview and it's working properly
- (void)viewDidLoad {
[super viewDidLoad];
SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
[mySwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
//[self.view addSubview:mySwitch];
mySwitch.on = true;
[_cntView addSubview:mySwitch];
SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
[mySwitch3 addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch3];
//self.view.backgroundColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
mySwitch3.borderColor = [UIColor clearColor];
mySwitch3.shadowColor = [UIColor blackColor];
[_cntView addSubview:mySwitch3];
}
- (void)switchChanged:(SevenSwitch *)sender {
NSLog(#"Changed value to: %#", sender.on ? #"ON" : #"OFF");
}
In which _cntView is the main container view which i have placed inside the scrollview , please check if this working for you
Update
As i mention in the comment i didn't getting what you are trying to say with touch tracking but i have made sample with tap gesture in the scrollview which may help
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
[scrollview setContentSize:CGSizeMake(self.view.frame.size.width,700)];
[self.view addSubview:scrollview];
SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
[mySwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
//[self.view addSubview:mySwitch];
mySwitch.on = true;
[scrollview addSubview:mySwitch];
SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
[mySwitch3 addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
mySwitch3.borderColor = [UIColor clearColor];
mySwitch3.shadowColor = [UIColor blackColor];
[scrollview addSubview:mySwitch3];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.cancelsTouchesInView = NO;
[scrollview addGestureRecognizer:singleTap];
}
- (void)actionSingleTap:(UITapGestureRecognizer *)sender {
NSLog(#"Tap");
}
- (void)switchChanged:(SevenSwitch *)sender {
NSLog(#"Changed value to: %#", sender.on ? #"ON" : #"OFF");
}
I have made all the new code programatically and it detects touch events outside the sevenSwitch and also detect touch/tap on the seven switch also.If you want to make it make scrollview in Storyboard and change the programatic scrollview with the storyboard outlet
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[self.scrollView addGestureRecognizer:singleTap];
}
- (void)actionSingleTap:(UITapGestureRecognizer *)sender {
CGPoint touchPoint = [sender locationInView:self.scrollView];
NSLog(#"Touch point coordinates are : %f - %f", touchPoint.x , touchPoint.y );
UIView *hitImageView = [self.scrollView hitTest:touchPoint withEvent:nil];
NSLog(#"%#", hitImageView);
switch (hitImageView.tag) {
case 1:
// do something
break;
default:
break;
}
}

UITapGestureRecognizer not working, Objective-C

I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;

Add callouts to custom infoMarkerWindow implementations for GMSMapview

I am using Google IOS SDK and have implemented
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
to implement a custom view. When I show this view, it does not add a callout as Google map view normally does for its own markers. Can anyone please let me know how it would be possible to add this callout like pointer pointing towards the marker for this custom makerInfoWindow ?
Here is how to do it.
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
int popupWidth = 300;
int contentWidth = 280;
int contentHeight = 140;
int contentPad = 10;
int popupHeight = 200;
int popupBottomPadding = 16;
int popupContentHeight = contentHeight - popupBottomPadding;
int buttonHeight = 30;
int anchorSize = 20;
CLLocationCoordinate2D anchor = marker.position;
CGPoint point = [mapView.projection pointForCoordinate:anchor];
UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)];
float offSet = anchorSize * M_SQRT2;
CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4); //rotate by 45 degrees
UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake((popupWidth - offSet)/2.0, popupHeight - offSet, anchorSize, anchorSize)];
callOut.transform = rotateBy45Degrees;
callOut.backgroundColor = [UIColor blackColor];
[outerView addSubview:callOut];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, 190)];
[view setBackgroundColor:[UIColor whiteColor]];
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = 2.0f;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 0, contentWidth, 22)];
[titleLabel setFont:[UIFont systemFontOfSize:17.0]];
titleLabel.text = [marker title];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 24, contentWidth, 80)];
[descriptionLabel setFont:[UIFont systemFontOfSize:12.0]];
descriptionLabel.numberOfLines = 5;
descriptionLabel.text = [marker snippet];
[view addSubview:titleLabel];
[view addSubview:descriptionLabel];
[outerView addSubview:view];
return outerView;
}
This method is a GMSMapViewDelegate method, did you set the delegate to the view controller?
mapView.delegate = self;

changing the border color of a label by tapping and dragging

I want to be able to change the border color of my label when it is tapped and dragged and when it is not being tapped and dragged, the border color should return to its previous colour. I have employed both pan and tap gesture recognisers but I have no idea in writing the code to change this. This is my code below:
(void)change:(id)sender {
CGRect labelFrame = CGRectMake(230, 240, 300, 30);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:labelFrame];
headingLabel.layer.borderColor = [UIColor clearColor].CGColor;
headingLabel.layer.borderWidth = 1.0;
headingLabel.backgroundColor = [UIColor blackColor];
headingLabel.textColor = [UIColor redColor];
[self.view addSubview:headingLabel];
[headingLabel setUserInteractionEnabled:YES];
headingLabel.text = _textField.text;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panDetected:)];
[self.view addGestureRecognizer:panRecognizer];
[headingLabel addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapDetected:)];
[self.view addGestureRecognizer:tapRecognizer];
[headingLabel addGestureRecognizer:tapRecognizer];
}
(void)panDetected:(UIPanGestureRecognizer *)paramSender{
if (paramSender.state != UIGestureRecognizerStateEnded &&
paramSender.state != UIGestureRecognizerStateFailed){
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
(void) tapDetected:(UITapGestureRecognizer *)paramSender {
NSUInteger touchCounter = 0;
for (touchCounter = 0;
touchCounter < paramSender.numberOfTouchesRequired;
touchCounter++){
CGPoint touchPoint =
[paramSender locationOfTouch:touchCounter
inView:paramSender.view];
}
}
#end
You have to use the view delegate methods :
touchesBegan, touchesEnd..
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

How to Rotate image after tapped in uiscrollview?

- (void)configureView
{
lblwelcome.text=#"Welcome To Gallery World";
for (UIImageView *subview in self.view.subviews) {
[subview removeFromSuperview];
}
UIBarButtonItem *addAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Zoom"
style:UIBarButtonItemStylePlain
target:self
action:#selector(zoomEnabled)];
UIBarButtonItem *delAcc = [[UIBarButtonItem alloc]
initWithTitle:#"Rotate"
style:UIBarButtonItemStylePlain
target:self
action:#selector(rotateEnabled)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:addAcc,delAcc, nil];
self.navigationItem.rightBarButtonItems = arrBtns;
categoryScrollX=30;
categoryScrollY=30;
[scr.delegate self];
scr.frame = self.view.frame;
for (UIImageView *subview in scr.subviews) {
[subview removeFromSuperview];
}
height = 200;
width = 400;
pos_y =0;
pos_x =0;
int idx=0;
CGFloat x=0;
switch (_itemNo) {
case 0:
for(int i=1;i<6;i++)
{//imageView.frame = CGRectMake(categoryScrollX, categoryScrollY, (self.view.frame.size.width/4)-25, self.view.frame.size.width/4-5);
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(categoryScrollX, categoryScrollY, (self.view.frame.size.width/4)-25, self.view.frame.size.width/4-5)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"e1%d.png",i]]];
[scr addSubview:image];
x+=320;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:tapGesture];
// [image.layer setBorderColor: [[UIColor yellowColor] CGColor]];
// [image.layer setBorderWidth: 0.9];
// image.layer.MasksToBounds = false;
// image.layer.ShadowColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
// image.layer.ShadowOpacity = 1.0f;
// image.layer.ShadowOffset = CGSizeMake(0, 2.5f);
// image.clipsToBounds = NO;
CALayer* layer = [image layer];
[layer setBorderWidth:2.0f];
[image setContentMode:UIViewContentModeScaleAspectFit];
[layer setBorderColor:[UIColor whiteColor].CGColor];
scr.minimumZoomScale = scr.frame.size.width / imgview.frame.size.width;
scr.maximumZoomScale = 2.0;
[scr setZoomScale:scr.minimumZoomScale];
scrollPosition++;
currentIndex++;
if(scrollPosition>4)
{
categoryScrollY = categoryScrollY+self.view.frame.size.width/4+12;
scrollPosition=0;
currentIndex = currentIndex+scrollPosition;
categoryScrollX=30;
}
else
{
categoryScrollX= (categoryScrollX+self.view.frame.size.width/4)-6;
}
}
scr.pagingEnabled=YES;
scr.contentSize = CGSizeMake(320*5, 300);
_detailDescriptionLabel.text=#"Accessories";
self.title=#"Accessories";
[self.view addSubview:scr];
break;
.....following by other cases with the above way with different images....
Above is my configure view and am just adding some dynamic images in UiScrollview and i have added tap guesture on each images....with the following
-(void)handleSingleTap:(UITapGestureRecognizer *)rec{
for (UIImageView *subview in scr.subviews) {
[subview removeFromSuperview];
}
UIView *img = [rec view];
img.frame = self.view.frame;
[self.view addSubview:img];
}
now what i want is after tap i want to give rotation effect to that image..
am just beginners for this Xcode environment and as well as iOS development.
some how i just succeeded to show images in uiscrollview..but after googled so much i couldn't find a good guidance for rotating the image.so can any one help me out to give rotation to uiimageview image in uiscrollview after the image is tapped?
try like this ,
-(IBAction)ButtonAction:(id)sender{
CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 1;
fullRotation.repeatCount = 1;
[imageview.layer addAnimation:fullRotation forKey:#"360"];
}
take one button and give action(call this mathod) for that button.
You can rotate the imageView using the following code :
imageView.transform = CGAffineTransformMakeRotation(angle);

Resources