i have a UITableViewCell, and i am trying to add an animation that causes a layer to scroll off of the cell, the problem is it gets clipped by the cell above it, is there an easy way to get around this?
here is my method.
- (void)animateLife:(UIButton *)sender isPositive:(BOOL)state{
// Reset lCount if we are pressing a different button than last time.
if (sent != sender) {
lCount = 0;
}
sent = sender;
// Increase the count by 1.
lCount = lCount + 1;
// Set up some properties.
CGPoint origin = sender.center;
CGPoint newOrigin = CGPointMake(origin.x, (origin.y - 100));
NSString *oper = [[NSString alloc] init];
// Check to see if this is a + or - change.
if (state == true) {
oper = #"+";
} else {
oper = #"-";
}
// Alloc a String and a Label to hold it.
NSString *characters = [[NSString alloc] initWithFormat:#"%#%d", oper, lCount];
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(origin.x,origin.y , 50, 50)];
// Configure the Label.
if (oper == #"-") {
[theLabel setTextColor:[UIColor redColor]];
} else {
[theLabel setTextColor:[UIColor greenColor]];
}
[theLabel setText:characters];
[theLabel setBackgroundColor:[UIColor clearColor]];
[theLabel setAlpha:1];
[theLabel setCenter:origin];
[theLabel setShadowColor:[UIColor blackColor]];
[theLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
[theLabel setFont:[UIFont fontWithName:#"Helvetica" size:28]];
[theLabel setTextAlignment:UITextAlignmentCenter];
[theLabel.layer setShadowRadius:3];
[theLabel.layer setShadowOpacity:0.9];
// Display our Label.
[sender.superview insertSubview:theLabel aboveSubview:sender];
// Setup animation.
[UIView beginAnimations:#"lifeCount" context:nil];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:)];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[theLabel setCenter:newOrigin];
[theLabel setAlpha:0];
// Commit Animation.
[UIView commitAnimations];
fCount = fCount + 1;
// Clean up.
[theLabel release];
[characters release];
[oper release];
}
- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// Since an animation finished, reduce count by one.
fCount = fCount - 1;
//check if all animations are finished, if so, reset the lCount.
if (fCount < 1) {
lCount = 0;
}
}
Update: it appears if i scroll cells to where they get reused, then the animation shows up properly is there a way i can make it not require the reuse?
In your method cellForRowAtIndexPath there is probably code like this:
cell = [tableView dequeueReusableCellWithIdentifier:someCellID];
if ( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellID] autorelease];
}
Do not call the "dequeueReusableCellWithIdentifier" method. Just create new cell each time.
Note that if you have a lot of rows in your table, this will have a performance hit.
Related
I am dynamically creating UILabels and then saving their tag in an NSMutableArray. I then have a method that detects taps (clicks) on these UILabels. Basically when a UILabel that has been dynamically generated is clicked I want to have it deleted without deleting other labels. However, in future I may want to do more then just delete. But at the moment I feel like I am stuck at a dead end trying to find a way to do this. Any ideas?
Heres my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// set corner radius
coverview.hidden=YES;
labeltextfield.hidden=YES;
textcreate.hidden=YES;
labeltags = [NSMutableArray array];
labeltext = [NSMutableArray array];
}
-(IBAction)removeboard
{
[labeltextfield resignFirstResponder];
}
-(void)showtextcreator {
// Create bg cover
coverview.hidden=NO;
labeltextfield.hidden=NO;
textcreate.hidden=NO;
//Make sure creating screen is always on top
[self.view bringSubviewToFront:coverview];
[self.view bringSubviewToFront:labeltextfield];
[self.view bringSubviewToFront:textcreate];
}
-(void)createtext {
NSInteger obj = [labeltags count] +1 ;
[labeltags addObject:[NSNumber numberWithInteger:0]];
int posx = arc4random() % 300 ;
int posy = arc4random() % 400 ;
int frame = arc4random() % 400 ;
NSString *txt = labeltextfield.text;
// NSString *framename = (#"frame%i",frame);
[labeltext addObject:txt];
[labeltags addObject:[NSNumber numberWithInteger:0]];
CGRect labelframe = CGRectMake( posx, posy, 100, 30);
label = [[UILabel alloc] initWithFrame: labelframe];
[label setText: [NSString stringWithFormat:#"%#", txt]];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor = [UIColor clearColor];
label.tag=obj;
[self.view addSubview: label];
label.userInteractionEnabled = YES;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[label addGestureRecognizer:gesture];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[label addGestureRecognizer:recognizer];
coverview.hidden=YES;
labeltextfield.hidden=YES;
textcreate.hidden=YES;
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
// reset translation
[gesture setTranslation:CGPointZero inView:label];
}
- (void)tapAction {
UILabel *labelnew = (UILabel *)[self.view viewWithTag:1];
NSLog(#"Text is %#",labelnew.text);
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[label addGestureRecognizer:recognizer];
- (void)tapAction:(UITapGestureRecognizer *)tapGesture {
UILabel *labelTapped = (UILabel *)tapGesture.view;
//delete it using removeFromSuperView or do whatever you need with tapped label
}
Details:
1.Modify your -(void)createtext method.
2: Add a parameter to the target for UITapGestureRecognizer
3.Receive the sender gesture in - (void)tapAction:
4.Get the tapped UILabel.
Thats it.
I am creating a app that display subview like ActionSheetView and in that i have added UIGridView and UIPageControll thru outlet , but now when i click the cell it does not respond ,my subview is a ViewController with the nib file.
and if i create a button and add in my popupview programmatically then it wil displaying, and also get the click event, but anything in outlet does not respond.
i have tried to set userinteraction enable in my subview but it does not done any good.
here is my code for first view that display subview by clicking barbutton:
- (void)popUpView:(id)sender {
//self.view.backgroundColor = [UIColor darkGrayColor];
bGView = [[UIView alloc] init]; /this is the background view and all view is added in it
bGView.frame = CGRectMake(0,0,320,490);
bGView.backgroundColor = [UIColor clearColor];
bGView.alpha = 0.8 ;
CGRect viewFrame = CGRectMake(10, 90, 300, 300);
aPopUpViewController = [[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil]; //this is my view controller and when this shows on screen it display my greed view
aPopUpViewController.view.frame = viewFrame;
aPopUpViewController.view.userInteractionEnabled = YES;
[bGView addSubview:aPopUpViewController.view];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor = [UIColor clearColor];
[button setBackgroundImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(-1, 80, 30, 30);
[bGView addSubview:button];
//for view animation
/*
// from bottom to center animation
[bGView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.4];
[bGView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];*/
//for page like effect
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
animations:^ { [self.view addSubview:bGView]; }
completion:nil];*/
/*CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionReveal; //choose your animation
[bGView.layer addAnimation:transition forKey:nil];
[self.view addSubview:bGView];*/
aPopUpViewController.view.userInteractionEnabled = YES;
bGView.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
bGView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
bGView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// do something once the animation finishes, put it here
}];
[self.view addSubview:bGView];
addLabel.enabled = NO;
clipArt.enabled = NO;
emailItem.enabled = NO;
closeButton.enabled = NO;
self.navigationController.topViewController.title = #"Choose ClipArt";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"no";
}
- (void)aMethod:(id)sender
{
//[aPopUpViewController.view removeFromSuperview];
//[button removeFromSuperview];
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve//change to whatever animation you like
animations:^ { [bGView removeFromSuperview]; }
completion:nil];*/
[bGView removeFromSuperview];
addLabel.enabled = YES;
clipArt.enabled = YES;
emailItem.enabled = YES;
closeButton.enabled = YES;
self.navigationController.topViewController.title = #"Greeting's";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"yes";
}
and this is how my screen look like
UPDATE
now i figured out my problem,problem is that i have given my whole view a tap listner,now i am adding grid view as subview so when ever i tap on cell rather then do select cell it will generate view tap gesture and thats why cell never selected,that's what i think..
add this line after you add every subviews in your main view..
[self.view bringSubviewToFront:yourGridView];
Since when I click the UITextField the date picker is showing along with the keyboard, I want to hide the key board operation on dob-text field?Here my code
- (void)removeViews:(id)object
{
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender
{
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView beginAnimations:#"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeViews:)];
[UIView commitAnimations];
}
- (IBAction)but
{
//[eventText resignFirstResponder];
[dob resignFirstResponder];
if ([self.view viewWithTag:9])
{
return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.tag = 10;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ;
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
//[datePicker addTarget:self action:#selector(dateText:)forControlEvents:UIControlEventValueChanged];
//NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
//_dateFormatter.dateStyle = NSDateFormatterFullStyle;
//dateText.text = [NSString stringWithFormat:#"%#",
// [_dateFormatter stringFromDate:datePicker.date]];
//[self.tableview reloadData];
}
I added the resignfirstresponder also,since its showing the same error
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];//dob textfield
}
Use UITextField's delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == dob-text )
//load picker here
return NO; //hide keyboard
else
return YES; //show keyboard
}
A better solution is to set the UIPickerView as the inputView for the UITextField.
And you can set the UIToolBar as the inputAccessoryView of the inputView.
This way iOS will handle the displaying of all UIPickView.
Just set the properties top the correct views:
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.inputToolbar;
On another note, getting views with viewWithTag: methods means that the you are looping thru all the views. Why not create properties for these views. It will be less messy and might be faster.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
datePicker.hidden = NO;
[textField resignFirstResponder];
}
try in textFieldDidBeginEditing
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
First of all a textfield should not have a popover - neither picker nor popover nor anything
Best solution would be to put a button - type custom - set image like a date selector image and add selector - in that case you would n't have any issues w.r.t textfields popover and the look would be good as well and you can customize it as per your requirement.
I have a custom uiview that i am trying to update the frame size. I do it like this in a method once it is clicked. The method is called, and the frame value changes, however on the screen nothing happens! What I think is happening is the subclass is not being redrawn or updated
- (void)resizeWithTag:(int)tag wasTouched:(BOOL)touched
{
if (!touched) {
GameBox *box = (GameBox *)[self.view viewWithTag:40];
CGRect frame = box.frame;
frame.origin.x += 30;
box.frame = frame;
[[self.view viewWithTag:40] setFrame:CGRectMake(20, 20, 100, 73)];
[box layoutSubviews];
NSLog(#"%f", box.frame.origin.x);
markButton.enabled = NO;
guessButton.enabled = NO;
[self.view reloadInputViews];
NSLog(#"The action bar should now be hidden");
}
else if (touched) {
guessButton.enabled = YES;
markButton.enabled = YES;
[self.view reloadInputViews];
NSLog(#"The action bar should now be visible");
}
}
I made a UIView subclass (bowl) and when I try to make a bowl within a bowl and then center the inner one on the outer one's center it end up getting placed below and to the right of the center. How would I properly center it?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
self.userInteractionEnabled = YES;
UIGestureRecognizer *twoFingerZ = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchZoom:)] ;
[self addGestureRecognizer:twoFingerZ];
UILabel *label = [[UILabel alloc] init];
[label setFont:[UIFont fontWithName:#"Arial" size:64]];
label.text = [NSString stringWithFormat:#"March"];
[label sizeToFit];
label.center = self.center;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor lightGrayColor];
self.backgroundColor = [UIColor whiteColor];
[self addSubview:label];
}
return self;
}
-(void)pinchZoom:(UIPinchGestureRecognizer *)recognizer
{
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) {
CGAffineTransform newForm = CGAffineTransformMakeScale(600/self.bounds.size.width, 600/self.bounds.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self setTransform:newForm];
return;
[UIView commitAnimations];
}
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
subClip = [[clipView alloc] initWithFrame:CGRectMake((self.center.x - ([delegate getSize:self].width/2)), (self.center.y - ([delegate getSize:self].height/2)), [delegate getSize:self].width, [delegate getSize:self].height)];
subClip.transform = CGAffineTransformMakeScale(0.1, 0.1);
[self addSubview:subClip];
bowl *newB = [[bowl alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
newB.center = self.center;
// right here! *************
[self addSubview:newB];
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
EDIT: I fixed it. Apparently the center value for a UIView isn't exactly what I thought it was (it looks like it's the position of the center in another view or something), so instead of newB.center = self.center, the right way to do it was newB.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
To center the inner label you should be able to use:
label.center=CGPointMake(self.center.x,self.center.y);
But first you'll probably want to define the frame size of the inner label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,yourWidth,yourHeight);
You may also want to center the text within the inner label:
label.textAlignment = UITextAlignmentCenter;
Also, not sure what you're intending in your code with this:
[label sizeToFit];
But according to the documentation, sizeToFit will resize and moves the receiver view so it encloses its subviews. In this case, it doesn't appear that your inner view has subviews. So your centering may be off because you may have unexpected dimensions for your inner label. Try setting the background color for that label to something you can see (i.e. not [UIColor clearColor]) to double check its dimensions. May help solve the centering problem.
Good luck.