Three20 TabStrip - ios

I am using TabStrip on a project of mine. The tabstrip has TTTabItems that are the alphabet A-Z. When I click to each alphabet letter the delegate navigates to another page.
In the other page there is a TTTabstrip also (same construction, frame etc). How can I mark the alphabet also on the second tabstrip without triggering the event?
I tried to get the integer value of the letter mod the integer value of "a" (so that it will bring me an index) and assign it to selectedIndex:
unichar letter = [[NSString stringWithString:#"e"] characterAtIndex:0];
/*(k is #"a" unichar)*/
tabBar.selectedTabIndex = letter % kEnglishAlpha;
But it gives me deallocation error.
** UPDATE **
on the drill down detail view (where I have the problem) I mentioned in the comments I have the following coming from a tt navigation:
tt://listWords/Cat
on the view did load event, and after the initialization of the TTTabStrip (that contains only TTTabItem with letters A-Z) and the tabBar.delegate = self declaration, I use the upper code to "select" the chosen letter. The rest of the view shows the definition and everything related. Normally that would be the case, but when I use the back button to return to the view that has the tttableItems (cat, catsup, catnip etc) it says deallocated controller.
My guess is that using the tabselected delegation code and setting a tabindex, it triggers the tab selected code immediate and that's where the problem is. If I don't use the selectedTabIndex code, I can navigate back and through the letters without problem.

There's no function to auto selected a TTTabItem in TTStripTab.
You will have to extend TTTabStrip and add this function:
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setSelectedTabIndexToCenter:(NSInteger)selectedTabIndex {
if (selectedTabIndex>_tabItems.count-1) {
selectedTabIndex = _tabViews.count-1;
}
[self layoutSubviews];
float horizontalOffset = 0.0f - _scrollView.size.width/2;
for (int i = 0; i < selectedTabIndex; ++i) {
TTTab* tab = [_tabViews objectAtIndex:i];
if (selectedTabIndex-1==i) {
horizontalOffset += tab.size.width*1.5;
} else {
horizontalOffset += tab.size.width;
}
}
if (horizontalOffset<0) {
horizontalOffset = 0;
}
_scrollView.contentOffset = CGPointMake(horizontalOffset, 0);
[super setSelectedTabIndex:selectedTabIndex];
}
This function will select a specific tab item and center on it by changing the offset of the strip tab.

Related

CorePlot "indexOfVisiblePointClosestToPlotAreaPoint" giving me wrong index

Currently I am plotting the graph with two data plots(two lines on the graph), when I click on one line "indexOfVisiblePointClosestToPlotAreaPoint" method giving me right index and other one throwing me wrong one, not even closest visible one its skipping/jumping multiple points in between. Ex: if I click on index number 20 it jumps to 15 or 25 vice versa. Here is the calculation to find the index
-(NSUInteger)indexOfVisiblePointClosestToPlotAreaPoint:(CGPoint)viewPoint
{
NSUInteger dataCount = self.cachedDataCount;
CGPoint *viewPoints = calloc(dataCount, sizeof(CGPoint));
BOOL *drawPointFlags = calloc(dataCount, sizeof(BOOL));
[self calculatePointsToDraw:drawPointFlags forPlotSpace:(CPTXYPlotSpace *)self.plotSpace includeVisiblePointsOnly:YES numberOfPoints:dataCount];
[self calculateViewPoints:viewPoints withDrawPointFlags:drawPointFlags numberOfPoints:dataCount];
NSInteger result = [self extremeDrawnPointIndexForFlags:drawPointFlags numberOfPoints:dataCount extremeNumIsLowerBound:YES];
if ( result != NSNotFound ) {
CGFloat minimumDistanceSquared = CPTNAN;
for ( NSUInteger i = (NSUInteger)result; i < dataCount; ++i ) {
if ( drawPointFlags[i] ) {
CGFloat distanceSquared = squareOfDistanceBetweenPoints(viewPoint, viewPoints[i]);
if ( isnan(minimumDistanceSquared) || (distanceSquared < minimumDistanceSquared)) {
minimumDistanceSquared = distanceSquared;
result = (NSInteger)i;
}
}
}
}
free(viewPoints);
free(drawPointFlags);
return (NSUInteger)result;
}
Are you checking the plot parameter passed in the delegate method? The touch will register on the frontmost plot if there are any points within the plotSymbolMarginForHitDetection. It won't even check the other plot unless nothing hits on the front one. With two lines close together like that, you'll need to use a small hit margin to make sure touches register on the right plot.

Actionscript. Tween all the textfield with the text inside it

In my actionscript 3 i have a textfield, which i fill with the messages (get them from the webservice). Not all the messages are visible, so every (for Example) 5s i'm moving them vertically to the top.
To do that, i'm using the Tween effect, but the thing is, that it moves only the visible text and not the rest text together (the invisible one).
When scrolling - i can see the rest text.
Image to show the situation (Sorry for my skills):
Here are some code: Function to fill messages object. Plus count how many lines every message will take to display ( so i could easilly use the tween effect (From - to))
private function getNewMsg(e:ResultEvent):void
{
size = e.result.toString().split(" ").length - 1;
for (var i=0; i<size; i++)
{
smsList.push(new smsItem(e.result[i].Id, e.result[i].text));
}
summ = 0;
for (var d=0; d<size; d++)
{
textfield.appendText(smsList[d].Text.toUpperCase()+'\n');
if (d >= 1)
{
smsList[d].Lines = textfield.numLines - summ;
summ += smsList[d].Lines;
}
else
{
smsList[d].Lines = textfield.numLines-1;
summ += smsList[d].Lines + 1;
}
}
twEnd = smsList[1].Lines * (-30.5);
}
And on timer i'm starting the Tween:
function timerHandler(e:TimerEvent):void
{
myTweenX = new Tween(textfield, "y", None.easeIn, twStart, twEnd, 4, true)
myTweenX.addEventListener(TweenEvent.MOTION_FINISH, tweenCompleted);
}
function tweenCompleted(e:TweenEvent):void
{
twInd++;
twStart = twEnd;
twEnd += smsList[twInd].Lines * (-30.5);
}
Found the problem. Because of my fixed textfield height, text wasn't. After made the textfields height auto size, evrything got fixed.

Getting caret (cursor) vertical position in a UIWebView

In order to manage automatic scrolling in a content editable UIWebView, I need to get the correct caret Y vertical coordinate.
I identified two methods, using javascript.
The first one uses getClientRects javascript function:
CGRect caretRect = [self.webView stringByEvaluatingJavaScriptFromString: #"
var sel = document.getSelection();
var range = sel.getRangeAt(0).cloneRange();
range.collapse(true);
var r =range.getClientRects()[0];
return '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}';"];
int caretY = caretRect.origin.y;
With this, the caret keeps blinking and one gets its correct vertical position with one problem: when user type return key, so that the next line is empty, caretY is equal to zero until a character key is typed. So that this method cannot be use.
The second one insert a tmp span, then removes it:
int caretY = [[self.webView stringByEvaluatingJavaScriptFromString: #"
var sel = window.getSelection();
sel.collapseToStart();
var range = sel.getRangeAt(0);
var span = document.createElement(\"span\");
range.insertNode(span);
var topPosition = span.offsetTop;
span.parentNode.removeChild(span);
topPosition;"] intValue];
This gives the correct caretY in any situation. But the caret stops blinking, which is very unhelpful to see it on the screen.
Does anybody knows any method (or adaptation of these) that can make the following:
- get the correct caret Y in any situation
- keep the caret blinking in any situation
Thanks
Not sure if this is too late; the following works for me.
Add getCaretY() below to your js file:
function getCaretY() {
var y = 0;
var sel = window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
if (range.getClientRects) {
var rects = range.getClientRects();
if (rects.length > 0) {
y = rects[0].top;
}
}
}
return y;
}
Then call it from obj-c file as below:
NSString *yPos = [webView stringByEvaluatingJavaScriptFromString:#"getCaretY()"];

Coreplot iOS - Custom space between graph bars

I want to know if it is possible to have custom spacing between bars after some fixed interval using Coreplot iOS library.
Like in the image below, after each 7 bars an unusual barspace is shown.
And if it is possible can you please guide how can this be achieved ?
CPTBarPlot has the code to manage this.
-(BOOL)barAtRecordIndex:(NSUInteger)idx basePoint:(CGPoint *)basePoint tipPoint:(CGPoint *)tipPoint
Basically gets the bar and sets its basePoint and tipPoint.
At the end, it is using barOffsetLength to offset each bar based on its index.
CGFloat barOffsetLength = [self lengthInView:self.barOffset] * self.barOffsetScale;
For vertical bars, in your case, its offsetting the x coord of base and tip point. These are usually the same. Here you have the choice of adding your own offset.
Simply, here's what you need to do there in the same function:
CGFloat barOffsetLength = [self lengthInView:self.barOffset] * self.barOffsetScale;
if ([self.dataSource hasGapBeforeIndex:idx]) {
offsetGap += [self.dataSource gapValue];
}
// Offset
if ( horizontalBars ) {
basePoint->y += barOffsetLength;
tipPoint->y += barOffsetLength;
}
else {
//HERO
basePoint->x += barOffsetLength + offsetGap;
tipPoint->x += barOffsetLength + offsetGap;
}
Here, you introduce a new variable in CPTBarPlot called offsetGap which gets increments everytime you introduce a gap. (be careful, this needs to be reset to zero when you change the dataset).
Also, in CPTPlotDataSource introduce
- (BOOL) hasGapBeforeIndex:(NSUInteger)index;
- (CGFloat) gapValue;
and implement it in your View Controller. Now you can introduce the gap anywhere.
PS: This obviously is a hack and upsets the axis labels and other things that might also need adjustment, but gives an overview anyway.
I played around with the sample app to achieve this.
You need to modify the positioning in your Core Plot data source method for the x axis
- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
and take into account where you want the spacing to occur. If you still don't get it, please post some code and I'll show you on that.
Logic example :
I want to represent the data for a month, lets say one that has 30 days, but at each 5 days, I want a pause at each 5 days. So instead of returning 30 in
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
, you return 34, and at indexes 6, 11, 16, 21 and 26 you return 0 for the method above.
You can extend this if you want not that much space for the 'pauses' and return double the amount of days (60), minus 4 (because for the pauses you return only for one record the value 0) and return for each 2 records the corresponding value in your data source. This can be again extended to your needed multiplier. I hope you got what I mean.
Thanks to #zakishaheen answer I managed to achieve this, but I broke label position and scroll content size 😄. This implementation is hacky thats why I decided not to continue with fixing it, its more just an example.
I created custom CustomOffsetBarPlot class and apply some Objective-C runtime magic.
- (BOOL)superImplementation:(SEL)selector idx:(NSUInteger)idx basePoint:(nonnull CGPoint *)basePoint tipPoint:(nonnull CGPoint *)tipPoint {
Class granny = [self superclass];
BOOL(* grannyImp)(id,SEL,NSUInteger,CGPoint*, CGPoint*) = (BOOL (*)(id,SEL,NSUInteger,CGPoint*, CGPoint*))class_getMethodImplementation(granny, selector);
return grannyImp(self, selector, idx, basePoint, tipPoint);
}
-(BOOL)barAtRecordIndex:(NSUInteger)idx basePoint:(nonnull CGPoint *)basePoint tipPoint:(nonnull CGPoint *)tipPoint {
SEL selector = _cmd;
CGPoint originBasePointStart = *basePoint;
CGPoint originTipPointStart = *tipPoint;
[self superImplementation:selector idx:0 basePoint:&originBasePointStart tipPoint:&originTipPointStart];
BOOL result = [self superImplementation:selector idx:idx basePoint:basePoint tipPoint:tipPoint];
Class granny = [self class];
SEL lengthView = NSSelectorFromString(#"lengthInView:");
CGFloat(* grannyImp)(id,SEL,NSDecimal) = (CGFloat (*)(id,SEL,NSDecimal))class_getMethodImplementation(granny, lengthView);
CGFloat barOffsetLengthOrigin = grannyImp(self, selector, self.barOffset.decimalValue);
NSInteger barOffsetLength = originBasePointStart.x + idx * 18 + idx * 5; // idx * 5 - your offset
basePoint->x = barOffsetLength;
tipPoint->x = barOffsetLength;
return result;
}

Bad to Set slider.value When User has Changed Slider Position?

In the online Stanford CS193p iPhone Application Development course, lecture 6, an application is built which has a slider as input and a custom view as output.
When the slider is changed, the view controller sets the slider value again.
Important bits of the view controller in Happiness 2.zip:
#implementation HappinessViewController
#synthesize happiness;
- (void)updateUI
{
// assignment-loop when called from happinessChanged:?
self.slider.value = self.happiness; // sets slider to model's (corrected) value
[self.faceView setNeedsDisplay];
}
- (void)setHappiness:(int)newHappiness
{
if (newHappiness < 0) newHappiness = 0; // limit value
if (newHappiness > 100) newHappiness = 100;
happiness = newHappiness;
[self updateUI]; // changed happiness should update view
}
- (IBAction)happinessChanged:(UISlider *)sender // called by changed slider
{
self.happiness = sender.value; // calls setter setHappiness:
}
Doesn't this result in a loop (slider changed -> model updated -> change slider -> ?)?
Or is this even good practice?
If the slider is updated from code, rather than by the user, it presumably doesn't sent the valueChanged action. So you don't get an infinite loop.
This can be used to "correct" the value selected by the user, or to force the slider onto regular tick marks instead of a smooth scale.

Resources