Objective C rateview not show half image - objective-c

I am using rateview like this page:
https://www.raywenderlich.com/1768/uiview-tutorial-for-ios-how-to-make-a-custom-uiview-in-ios-5-a-5-star-rating-view
It runs normally, but I cannot see half image show, like 1.5- 1.5-3.5..
Can someone help me know why? Thanks.

I fixed this problem, just add some code in handlingTouchFunction
- (void)handleTouchAtLocation:(CGPoint)touchLocation {
if (!self.editable) return;
double newRating = 0;
for(int i = self.imageViews.count - 1; i >= 0; i--) {
UIImageView *imageView = [self.imageViews objectAtIndex:i];
if (touchLocation.x > (imageView.frame.origin.x)) {
if(touchLocation.x <= (imageView.frame.origin.x + imageView.frame.size.width / 2))
newRating = i + 0.5;
else
newRating = i + 1;
break;
}
}
self.rating = newRating;
}

Use FloatRatingView for Whole, half or floating point ratings control.

Related

Project Euler # 10 in Objective C

I'm trying to solve Problem 10 in Project Euler, and while I thought I had it, its saying my answer is incorrect. The question is as follows:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
And my code:
int sum;
#interface Prime : NSObject
-(BOOL)isPrime:(int)arg1;
#end
#implementation Prime
-(BOOL)isPrime:(int)arg1 {
if (arg1 == 1) {
NSLog(#"Given 1");
return NO;
}
for (int i = 2; i < arg1; i++) {
if (arg1 % i == 0) {
return NO;
}
}
sum += arg1;
return YES;
}
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
Prime* primeObject = [[Prime alloc] init];
for (int i = 0; i < 2000000; i++) {
[primeObject isPrime:i];
}
NSLog(#"Sum of primes is %i", sum);
}
}
This code outputs 'Sum of primes is 1179908154' which Project Euler says is incorrect. Help?
The problem is that the sum does not fit into a 32-bit integer. You should use long long instead.
Just a guess, you should try to:
Initialise the sum variable to 0.
Try not to use a global variable like sum that can be accessed from anywhere, in this case do the sum in the main loop instead of in the isPrime method.
Maybe that'll give you the right answer.
You are using int for getting result, so it is wrong.
I'm using long int instead, that is enough for this case.
Here is my code, and it works fine:
int inputNumber = 2000000;
long int result = 0;
for (int i = 2; i < inputNumber; i++) {
BOOL isPrime = YES;
for (int j = 2; j <= sqrt(i); j++) {
if (i%j==0) {
isPrime = NO;
break;
}
}
if (isPrime) {
result += i;
}
}
Result is: 142913828922

How to make a rope with SpriteKit's SKPhysicsJointLimit?

I am using a for loop to make 7 rope link sprites, can't figure out how to make rope out of them with SKPhysicsJointLimit. :'(
-(void)ropeStuff {
int i ;
int y;
SKSpriteNode *ropes;
SKPhysicsJointLimit * ropeLink;
NSMutableArray *ropeArray;
for (i = 0 ; i < 7; ++i) {
if (i) {
int x = 16;
y = (x * i);
ropes.position = CGPointMake(_cat.position.x, _cat.position.y + (x * i) );
}
ropes = [SKSpriteNode node];
ropes = [SKSpriteNode spriteNodeWithImageNamed:#"rope link.png"];
ropes.position = CGPointMake(_cat.position.x, _cat.position.y +5);
ropes.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];
ropes.physicsBody.affectedByGravity = YES;
ropes.physicsBody.dynamic = YES;
ropes.name = #"rope";
[_worldNode addChild:ropes];
if (i) {
ropeLink = [SKPhysicsJointLimit jointWithBodyA:ropes.physicsBody
bodyB:ropes.physicsBody anchorA:ropes.position anchorB:ropes.position];
[_worldNode.scene.physicsWorld addJoint:ropeLink];
}
}
}
Thank you all for the help! :D
Why are you using SKPhysicsJointLimit? The rope is a group of segments that rotate relatively each other. You should use SKPhysicsJointPin with or without rotation limits.

Adding Alpha-Beta prunning to minMax in Objective-C

Hi there I have problems adding alpha beta pruning to my minMax algorithm for the connect 4 game can you help me ? here is my code for the minMax procedure, it looks to me that there is just a little that have to be done what eludes me :S scoreBoard() is my Heuristics function and I returning an array with 2 values the first one is the position of on the table and the other one is the score for this position.
-(NSArray*) miniMaxWihtAlphaBetaPrunning:(BOOL)maxOrMin withAlpha:(NSInteger)alpha
withBeta:(NSInteger)beta withPlayer:(enum playerColor)player andTreeDepth:
(NSInteger)depth
{
if (depth == 0)
{
return [NSArray arrayWithObjects:[NSNumber numberWithInt:-1],
[NSNumber numberWithInt:scoreBoard()],nil];
}
else
{
NSInteger bestScore = maxOrMin ? redWins: blueWins;
NSInteger bestMove = -1;
for (NSInteger column = 0; column < 10; column++)
{
if (discPlacedMatrix[0][column] != 0)
{
continue;
}
NSInteger rowFilled = dropDiscAtPoint(column, player);
if (rowFilled == -1)
{
continue;
}
NSInteger s = scoreBoard();
if (s == (maxOrMin? blueWins : redWins))
{
bestMove = column;
bestScore = s;
discPlacedMatrix[rowFilled][column] = 0;
break;
}
NSArray* result = [NSArray arrayWithArray:[self miniMaxWihtAlphaBetaPrunning:!maxOrMin withAlpha:alpha withBeta:beta withPlayer:(player == 1 ? RED : BLUE) andTreeDepth:depth - 1]];
NSInteger scoreInner = [[result objectAtIndex:1] intValue];
discPlacedMatrix[rowFilled][column] = 0;
if (scoreInner == blueWins || scoreInner == redWins)
{
scoreInner -= depth * player;
}
if (maxOrMin)
{
if (scoreInner >= bestScore)
{
bestScore = scoreInner;
bestMove = column;
}
}
else
{
if (scoreInner <= bestScore)
{
bestScore = scoreInner;
bestMove = column;
}
}
}
return [NSArray arrayWithObjects:[NSNumber numberWithInt:bestMove],[NSNumber numberWithInt:bestScore],nil];
}
}
I tried some scenarios but the Ai started to
It sounds like you're looking for something like the jamboree algorithm. The basic idea of this algorithm is to go through your tree recursively, and at each level, run the alpha beta algorithm on part of the nodes at that level, and then run the mini-max algorithm on the rest of the nodes. I'm at work right now, but I'll elaborate when I get home.
An implementation:
http://chessprogramming.wikispaces.com/Jamboree

Logically Layout Multiple Items into a View by their X and Y coordinates

I have a UIView and I want to add a series of programmatically generated UIButtons to it.
I've created a method that places each button (of standard size) via a CGPoint.
I want to lay these buttons out 3 in a row, over as many rows as needed.
I've no idea how to go about programatically generating the x and y coordinates to place the items on the view. I'm stuck on how to get the buttons to drop onto a new row once three have been placed.
Here's my code, I want to process something in this method to return the coordinates based only upon the index of the button:
- (CGPoint)calculateCoordinatesWithIndex:(NSInteger)index{
NSInteger x = 100;
NSInteger y = 50;
return CGPointMake(x, y);
}
The only solutions I can think of seem really clunky and inefficient.
Hello what about something like :
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 50
- (void) placeButtonsFromButtonsArray:(NSArray *) buttons{
CGFloat currentXPosition = 0.0;
CGFloat currentYPosition = 0.0;
for (NSUInteger i = 0; i < [buttons count]; i++){
b.frame = CGRectMake(currentXPosition, currentYPosition, BUTTON_WIDTH, BUTTON_HEIGHT);
if (i%3 == 2){
currentXPosition = 0;
currentYPostion += BUTTON_HEIGHT;
}
else currentXPosition += BUTTON_WIDTH;
}
}
or Using your method :
- (CGPoint)calculateCoordinatesWithIndex:(NSInteger)index{
//computes on which line the button should be
NSUInteger line = (index/3);
//computes on which row the button should be
NSUInteger row = (index%3) ;
return CGPointMake(row*BUTTON_WIDTH, line*BUTTON_HEIGHT);
}
If you're targeting iOS 6, you could use a UICollectionView for this.
try this,
in .h
{
NSInteger x;
NSInteger y;
int noOfbtninrow;
}
-(void)viewDidload
{
x = 100;
y = 50;
noOfbtninrow=0;
}
- (CGPoint)calculateCoordinatesWithIndex:(NSInteger)index
{
if(noOfbtninrow==3)
{
x=100;
y=y+100
noOfbtninrow=0;
}
else
{
noOfbtninrow++;
x=x+100;
}
return CGPointMake(x, y);
}

Loop to make grid of NSButtons?

I am trying to loop through and programmatically make a grid of 256 NSButtons (16x16). The code I have so far is below. This is in Objective-C for my Mac app. So I am logging to see which tag I get when I click a button, but it keep returning the same tag every time.
I want it so that every button goes 1-256 from left to right, top to bottom. This code successfully makes them load into my view, but the tags are wrong.
#define N_ROWS 16
#define N_COLS 16
int btnSpaceDifference = 1;
int btnSpacing = N_ROWS + btnSpaceDifference;
for (int j = 0; j < N_ROWS; j++) {
for (int i = 0; i < N_COLS; i++) {
paintPixel = [[[NSButton alloc] initWithFrame:NSMakeRect(10 + (i * btnSpacing), 10 + (j * btnSpacing), 16, 16)] autorelease];
[paintPixel setTitle:#""];
[paintPixel setBezelStyle:NSBorderlessWindowMask];
[paintPixel setTag:j + i * N_ROWS + 1];
[paintPixel setAction:#selector(btnPaintAction:)];
[[[box.tabViewItems objectAtIndex:0]view] addSubview:paintPixel];
}
}
-(void)btnPaintAction:(id)sender{
NSLog(#"%ld", paintPixel.tag);
}
Instead of making all of these buttons yourself, why not use an NSMatrix? This is the sort of thing it's perfect for.
Not sure how its compiling, you might have paintPixel defined elsewhere. But you need to change your btnPaintAction from:
-(void)btnPaintAction:(id)sender {
NSLog(#"%ld", paintPixel.tag);
}
To something like this:
-(void)btnPaintAction:(id)sender {
NSButton * myButton = (NSButton *) sender;
NSLog(#"%ld", myButton.tag);
}
call setTag with an increment variable
int TagVal = 1;
for (int j = 0; j < N_ROWS; j++) {
....
[paintPixel setTag:TagVal++];
....
}
Then modify your btnPaintAction:
UIButton *button = (UIButton *)sender;
NSLog(#"%ld", button.tag);
It's returning the same tag every time because your action is referring to your (apparently) member variable paintPixel. Use the sender parameter to the action instead.
NSLog(#"%ld", ((NSButton *)sender).tag);
This is old post but I see no correct answer, thus my addition.
Q. " I want it so that every button goes 1-256 from left to right, top to bottom."
Kevin was on the good track, however one more alteration is required:
paintPixel = [[[NSButton alloc] initWithFrame:NSMakeRect(10 + (i * btnSpacing), 10 - (j * btnSpacing), 16, 16)] autorelease];
Thus minus(-) instead of plus(+) results in numbering top to bottom.