Dose anybody know how to animate text blinking in UITextView?
Regarding to blinking animation, I would like to use couple of colour variations in NSArray,,,
This is my current methods for it:
- (void)startFontColorFlashing:(UITextView *)textView{
[UIView animateKeyframesWithDuration:2.0
delay:0.0
options:UIViewKeyframeAnimationOptionRepeat | UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
NSLog(#"Font flash animation start");
NSArray *fontFlashColorsArr = #[[UIColor redColor], [UIColor blueColor]];
NSUInteger colorCount = [fontFlashColorsArr count];
for (NSUInteger i = 0; i < colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i/(CGFloat)colorCount
relativeDuration:1/(CGFloat)colorCount
animations:^{
textView.textColor = fontFlashColorsArr[i];
}];
}
}
completion:^(BOOL finished){
NSLog(#"Finished the animation! %d", finished);
}];
}
It should works, but as soon as staring the animation it will be finished.
Please give me a good tip!
Cheers,
I experienced difficulty attempting to animate the text color changing, just like you did. I'm not sure if this is the best solution, but it works for what you are trying to do. Just add a way to stop changeTextColor from being called an infinite number of times, like a global variable, and this should do the trick
- (void)textViewDidBeginEditing:(UITextView *)textView {
[self changeTextColor:textView];
}
- (void)changeTextColor:(UITextView *)textView {
[self performSelector:#selector(changeTextColor:) withObject:textView afterDelay:0.3];
if (textView.textColor == [UIColor redColor]) {
textView.textColor = [UIColor blueColor];
} else {
textView.textColor = [UIColor redColor];
}
}
Here is some updated code to use multiple colors. This assumes self.count is a global variable that is handling the number of times to blink. This is probably best done in a subclass of UITextView if you are using more than one in the same controller.
- (NSArray *)colors {
return #[[UIColor redColor], [UIColor blueColor], [UIColor greenColor]];
}
- (void)changeTextColor:(UITextView *)textView {
if (self.count < 20) {
[self performSelector:#selector(changeTextColor:) withObject:textView afterDelay:0.3];
} else {
self.count = 0;
return;
}
NSInteger colorIndex = self.count % 3;
self.textView.textColor = self.colors[colorIndex];
self.count++;
}
Related
I have a UILabe for my matching card game that writs "Good match!" for example if you have a match, and I want to make it disappear after notifying the user with the message.
What method do that? can I make it fad away..? thanks.
This is my updateUI method:
-(void) updateUI {
for (UIButton *cardButton in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
[cardButton setTitle:card.contents forState:UIControlStateSelected];
[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
cardButton.selected = card.isFaceUp;
cardButton.enabled = !card.unplayble;
if (card.unplayble) {
cardButton.alpha = 0.1;
}
self.scoreCounter.text = [NSString stringWithFormat:#"Score: %d", self.game.score];
if (self.game.notification) {
[UIView animateWithDuration:2 animations:^{
self.notificationLabel.text = [NSString stringWithFormat:#"%#", self.game.notification];
self.notificationLabel.alpha = 0;
}];
}
}
}
This will fade away the label over two seconds. Change the animateWithDuration: to however long you want
if (self.game.notification == nil) {
[UIView animateWithDuration:2 animations:^{
self.notificationLabel.alpha = 0;
}];
}
If you want to change the text, try this (I can't test it right now, so it might not work)
[UIView animateWithDuration:2 animations:^{
self.notificationLabel.text = #"text you want to change it to";
}];
And then, when you want to use the label again, you have to set the self.notificationLabel.alpha back to 1. So try
if (self.game.notification) {
self.notificationLabel.alpha = 1;
[UIView animateWithDuration:2 animations:^{
self.notificationLabel.text = [NSString stringWithFormat:#"%#", self.game.notification];
self.notificationLabel.alpha = 0;
}];
}
I don't think that's possible. More than likely, you'll need to replace the UILabel with a NSView in Interface Builder and use the drawing functions contained therein to draw the text with transparency.
I made a view which holds a UIScrollview:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 65, 300, 188)];
//BackViews will hold the Back Image
BackViews = [[NSMutableArray alloc] init];
for (int i=0; i<BigPictures.count; i++) {
[BackViews addObject:[NSNull null]];
}
FrontViews = [[NSMutableArray alloc] initWithCapacity:BigPictures.count];
[self.pageControl setNumberOfPages:BigPictures.count];
Then I add several UIImageviews containing images:
//BigPictures holds objects of type UIImage
for (int i = 0; i < BigPictures.count; i++) {
UIImageView *ImageView = [[UIImageView alloc] initWithImage:[BigPictures objectAtIndex:i]];
ImageView.frame = [self.scrollView bounds];
[ImageView setFrame:CGRectMake(self.scrollView.frame.size.width * i, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height)];
//this saves the FrontView for later (flip)
[FrontViews insertObject:ImageView atIndex:i];
[self.scrollView addSubview:test];
}
// Detect Single Taps on ScrollView
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(flip)];
[self.scrollView addGestureRecognizer:tap];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
Ok so far so good. Now the method which does the flipImage part:
- (void)flip {
int currentPage = self.pageControl.currentPage;
UIView *Back = nil;
if ([BackViews objectAtIndex:currentPage] == [NSNull null]) {
//CreateBackView is just creating an UIView with text in it.
Back = [self CreateBackView];
[BackViews replaceObjectAtIndex:currentPage withObject:Back];
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[BackViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
} else {
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[FrontViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL];
[BackViews replaceObjectAtIndex:currentPage withObject:[NSNull null]];
}
[self.view addSubview:Back];
[self rebuildScrollView];
}
This is what rebuildScrollView does:
- (void)rebuildScrollView
{
for (UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 0; i < BigPictures.count; i++) {
if ([BackViews objectAtIndex:i] == [NSNull null]) {
[self.scrollView addSubview:[FrontViews objectAtIndex:i]];
} else {
[self.scrollView addSubview:[BackViews objectAtIndex:i]];
}
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}
So the behavior is the following:
If I click on the first image (1 of 3 in scrollview) the effect is the way I want it, meaning the frontimage turns around and shows the empty (white) back with some text in the middle
If I click on the second image, the image turns but the back is completely empty showing the grey background of the window. If I scroll to the other images, the still show the front image (as expected)
Now I click on the third image and its the same as 1) great.
Current layout is now [BackView, Nothing, Backview)
Lets run that again. But now I click on the last image and its the same as 2) :(
Any ideas whats going wrong here?
EDIT: Some new findings. I doubled the amount of pictures and this is how Front and Backviews are placed (after flipping each one). P = Picture & B = Backview.
P_1(B_1) - actually the only correct one
P_2(empty - should be B_2)
P_3(B_2 - should be B_3)
P_4(empty - should be B_4)
P_5(B_3 - should be B_5)
P_6(empty - should be B_6)
Did a complete rebuild and now it works. Really strange because I used the exact same code.
#pragma mark - iAd method.
/*Starts:iAd*/
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
[self layoutForCurrentOrientation:YES];
}
}
-(void)createADBannerView
{
NSString *contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierPortrait;
CGRect frame;
frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
frame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.bounds));
// Now to create and configure the banner view
ADBannerView *bannerView = [[ADBannerView alloc] initWithFrame:frame];
// Set the delegate to self, so that we are notified of ad responses.
bannerView.delegate = self;
// Set the autoresizing mask so that the banner is pinned to the bottom
bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierPortrait, nil];
// At this point the ad banner is now be visible and looking for an ad.
[self.view addSubview:bannerView];
self.banner = bannerView;
[bannerView release];
}
-(void)layoutForCurrentOrientation:(BOOL)animated
{
CGFloat animationDuration = animated ? 0.2 : 0.0;
// by default content consumes the entire view area
CGRect contentFrame = self.view.bounds;
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
CGFloat bannerHeight = 0.0;
banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
bannerHeight = 55.0;
if(banner.bannerLoaded)
{
contentFrame.size.height -= bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
bannerOrigin.y += bannerHeight;
}
// And finally animate the changes, running layout for the content view if required.
NSLog(#"[UIApplication sharedApplication].statusBarOrientation = %d",[UIApplication sharedApplication].statusBarOrientation);
[UIView animateWithDuration:animationDuration
animations:^{
contentView.frame = contentFrame;
[contentView layoutIfNeeded];
banner.frame = CGRectMake(0,361, banner.frame.size.width, banner.frame.size.height);
}];
}
/*Ends:iAd*/
/*Starts:Added By:CP.Date:26-Dec-2010.
AdMob methods*/
#pragma mark - AdMobDelegate methods
-(NSString *) publisherIdForAd:(AdMobView *)adView
{
return #"a14f91029b8c719"; // this should be prefilled; if not, get it from www.admob.com
}
-(UIViewController *) currentViewControllerForAd:(AdMobView *)adView
{
return self;
}
-(UIColor *)adBackgroundColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)primaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(UIColor *)secondaryTextColorForAd:(AdMobView *)adView
{
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
-(void) didReceiveAd:(AdMobView *)adView
{
adMobAd.frame = CGRectMake(0,361,320,55);
//adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_748x110 withDelegate:self];
[self.view addSubview:adMobAd];
}
// Sent when an ad request failed to load an ad
-(void)didFailToReceiveAd:(AdMobView *)adView
{
Class cls = NSClassFromString(#"ADBannerView");
if(cls!=nil)
{
if(banner == nil)
{
[self createADBannerView];
}
[self layoutForCurrentOrientation:NO];
}
[adMobAd removeFromSuperview]; // Not necessary since never added to a view, but doesn't hurt and is good practice
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
/*Ends:Added By:CP.Date:26-Dec-2010.
AdMob methods*/
first of all be clear about iad or admob , that are two different plateform for displaing adv in your app.
i think you are try to integrate admob adv. you need to following steps for it.
http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-admob-integrate-in-your-application-in-iphone/
Its easy change the color of UISegmentedControl. I found various solution like this, this site and the best this solution. But none was what I want.
I tried create a simple thing and it work very easy, this was my code: (I am using the iOS 4.2, not the 5.0 and xcode 4.0.2)
id segment[3];
UISegmentedControl *segmentedControl;
- (id)init
{
NSArray *itens = [NSArray arrayWithObjects: #"Option 1", #"Option 2", #"Option 3", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itens];
[segmentedControl setFrame:CGRectMake(0, 0, 500, 30)];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentedControl addTarget:self
action:#selector(segmentedControl:)
forControlEvents:UIControlEventAllEvents];
switch (type) {
case type1: [segmentedControl setSelectedSegmentIndex:0]; break;
case type2: [segmentedControl setSelectedSegmentIndex:1]; break;
case type3: [segmentedControl setSelectedSegmentIndex:2]; break;
}
for (int i=0; i<3; i++) {
//The most important trick to work, have to retain the subviews
segment[i] = [[[segmentedControl subviews] objectAtIndex:i] retain];
}
[self changeColor];
[self addSubview:segmentedControl];
return self;
}
- (void)segmentedControl:(id)sender
{
//do some thing
[self changeColor];
}
- (void)changeColor{
for (int i=0; i<3; i++) {
[segment[i] setTintColor:[UIColor lightGrayColor]];
}
int select = segmentedControl.selectedSegmentIndex;
[segment[select] setTintColor:[UIColor blueColor]];
}
So it create this:
Very good, then I click in Option 2
Wow, this is exacly what I want, so click in Option 3
Now the problem, this stupid blue line (marked in red square) between Option 1 and Option 2. If I click in Option 1 again, I will have:
Than the blue line appear again. This mean that every left side on old clicked segment (but not with the first) will have this blue line. If I go from right to left it not happens.
I have no idea how to solve this. How can I access this line and change your color? Or I will have to use other codes. Maybe they will have the same problem...
I Think there is a lot easier solution.
Just clean the pointers..
for (int i=0; i<[self.segmentedControll.subviews count]; i++)
{
[[self.segmentedControll.subviews objectAtIndex:i] setTintColor:nil];
if (![[self.segmentedControll.subviews objectAtIndex:i]isSelected])
{
UIColor *tintcolor=[UIColor blackColor];
[[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor];
}
else
{
UIColor *tintcolor=[UIColor blueColor];
[[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor];
}
}
Wow... When we write were in stackoverflow, we can calm down and think better. I write the answer in my ow question:
If I go from right to left it not happens.
This is the solution! What I have to do? Simulate this.
BOOL inside = FALSE;
- (void)changeColor{
int old = segmentedControl.selectedSegmentIndex;
for (int i=0; i<3; i++) {
[segment[i] setTintColor:[UIColor lightGrayColor]];
}
/**/inside = TRUE;
/**/for (int i=3; i>0; i--) {
/**/ //When I do this, it call "segmentedControl:"
/**/ [segmentedControl setSelectedSegmentIndex:i];
/**/}
/**/int select = old;
[segment[select] setTintColor:[UIColor blueColor]];
[segmentedControl setSelectedSegmentIndex:select];
/**/inside = FALSE;
}
- (void)segmentedControl:(id)sender
{
/**/if (inside==TRUE) return; //Ignore this calls.
//do some thing
[self changeColor];
}
I am developing a Cocoa application and encountered a problem with highlighting. Standard highlighting color in MAC OS X applications is blue, but it doesn't suit my app, since because of design concepts, I need a green color for highlighting.
I tried to subclass NSTableview and override method
- (void)highlightSelectionInClipRect:(NSRect)clipRect
but it didn't help.
How to fix this problem?
I am using this, and so far works perfectly:
- (void)highlightSelectionInClipRect:(NSRect)theClipRect
{
// this method is asking us to draw the hightlights for
// all of the selected rows that are visible inside theClipRect
// 1. get the range of row indexes that are currently visible
// 2. get a list of selected rows
// 3. iterate over the visible rows and if their index is selected
// 4. draw our custom highlight in the rect of that row.
NSRange aVisibleRowIndexes = [self rowsInRect:theClipRect];
NSIndexSet * aSelectedRowIndexes = [self selectedRowIndexes];
int aRow = aVisibleRowIndexes.location;
int anEndRow = aRow + aVisibleRowIndexes.length;
NSGradient * gradient;
NSColor * pathColor;
// if the view is focused, use highlight color, otherwise use the out-of-focus highlight color
if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow])
{
gradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:(float)62/255 green:(float)133/255 blue:(float)197/255 alpha:1.0], 0.0,
[NSColor colorWithDeviceRed:(float)48/255 green:(float)95/255 blue:(float)152/255 alpha:1.0], 1.0, nil] retain]; //160 80
pathColor = [[NSColor colorWithDeviceRed:(float)48/255 green:(float)95/255 blue:(float)152/255 alpha:1.0] retain];
}
else
{
gradient = [[[NSGradient alloc] initWithColorsAndLocations:
[NSColor colorWithDeviceRed:(float)190/255 green:(float)190/255 blue:(float)190/255 alpha:1.0], 0.0,
[NSColor colorWithDeviceRed:(float)150/255 green:(float)150/255 blue:(float)150/255 alpha:1.0], 1.0, nil] retain];
pathColor = [[NSColor colorWithDeviceRed:(float)150/255 green:(float)150/255 blue:(float)150/255 alpha:1.0] retain];
}
// draw highlight for the visible, selected rows
for (aRow; aRow < anEndRow; aRow++)
{
if([aSelectedRowIndexes containsIndex:aRow])
{
NSRect aRowRect = NSInsetRect([self rectOfRow:aRow], 1, 4); //first is horizontal, second is vertical
NSBezierPath * path = [NSBezierPath bezierPathWithRoundedRect:aRowRect xRadius:4.0 yRadius:4.0]; //6.0
[path setLineWidth: 2];
[pathColor set];
[path stroke];
[gradient drawInBezierPath:path angle:90];
}
}
}
I searched for hours for an answer on this as well, and although I found many fragments, none of them were complete. So here I submit another approach, which I am using with success.
1) Set your NSTableView selectionHighLightStyle to None
This is necessary to ensure that OSX does not simply apply it's own highlights over the top of yours, leaving you with a blue highlight.
You can do this either through IB or via code.
2) Subclass NSTableView, and override drawRow.
This will set the background color for your selected rows to primary (active window) and secondary (inactive).
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
NSColor* bgColor = Nil;
if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow])
{
bgColor = [NSColor colorWithCalibratedWhite:0.300 alpha:1.000];
}
else
{
bgColor = [NSColor colorWithCalibratedWhite:0.800 alpha:1.000];
}
NSIndexSet* selectedRowIndexes = [self selectedRowIndexes];
if ([selectedRowIndexes containsIndex:row])
{
[bgColor setFill];
NSRectFill([self rectOfRow:row]);
}
[super drawRow:row clipRect:clipRect];
}
3) Implement an NSTableViewDelegate, attach it to your NSTableView, and implement willDisplayCell.
This will allow you to change the textColor of the rows on selection/deselection, in case your selection colors make the text hard to read.
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
// check if it is a textfield cell
if ([aCell isKindOfClass:[NSTextFieldCell class]])
{
NSTextFieldCell* tCell = (NSTextFieldCell*)aCell;
// check if it is selected
if ([[aTableView selectedRowIndexes] containsIndex:rowIndex])
{
tCell.textColor = [NSColor whiteColor];
}
else
{
tCell.textColor = [NSColor blackColor];
}
}
}
And you are done.
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)controlView
{
if ([self isHighlighted])
{
NSRect bgFrame = frame;
[[NSColor redColor] set];
NSRectFill(bgFrame);
}
}
I use this code to deal with the height, the code is in my custom cell file