Mostrando postagens com marcador Objective-C. Mostrar todas as postagens
Mostrando postagens com marcador Objective-C. Mostrar todas as postagens

sexta-feira, 20 de julho de 2012

Enum - Bit Operators


enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;






quarta-feira, 11 de julho de 2012

Objective-C em expansão


Pessoal, a linguagem de programação a que este blog se dedica está em franco crescimento.

Fonte: Tiobe

quarta-feira, 4 de julho de 2012

Função - Retorna uma NSString (hh:mm:ss) passando apenas os segundos

Função que retorna uma string (NSString) no formato de horário apenas passando a quantidade de segundos.
Barbada galera!



  1. +(NSString *)clockFormatWithSeconds:(int)seconds
  2. {
  3.     int hour = 0, min = 0, sec = 0;
  4.     NSString *h, *m, *s;
  5.  
  6.     sec = seconds;
  7.  
  8.     while (sec >= 60) {min+=1; sec-=60;}
  9.     while (min >= 60) {hour+=1; min-=60;}
  10.  
  11.     h = (hour > 9) ? [NSString stringWithFormat:@"%d",hour] : [NSString stringWithFormat:@"0%d",hour] ;
  12.     m = (min > 9) ? [NSString stringWithFormat:@"%d",min] : [NSString stringWithFormat:@"0%d",min] ;
  13.     s = (sec > 9) ? [NSString stringWithFormat:@"%d",sec] : [NSString stringWithFormat:@"0%d",sec] ;
  14.  
  15.     return [NSString stringWithFormat:@"%@:%@:%@",h,m,s];
  16. }


sexta-feira, 29 de junho de 2012

Função - Redimensionar UIImage

Função para redimensionar  imagens (UIImage).


  1. +(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
  2. {
  3.     UIGraphicsBeginImageContext(newSize);
  4.     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  5.     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  6.     UIGraphicsEndImageContext();
  7.     return newImage;
  8. }




segunda-feira, 25 de junho de 2012

Função - Embaralhar NSMutableArray

Função para você embaralhar um NSMutableArray um determinado número de vezes.

  1. //Aplicação do método
  2. NSMutableArray * array = [[NSMutableArray alloc]  initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
  3. [self shuffleMutableArray:&array frequency:10];
  4.  
  5. //Método para embaralhar arrays
  6. +(void)shuffleMutableArray:(NSMutableArray **)array frequency:(int)f
  7. {    
  8.     for (int i = 0; i < f; i++) {
  9.         int randA,randB;
  10.         do {
  11.             randA = (random() % [*array count] -i)+i;
  12.             randB = (random() % [*array count] -i)+i;
  13.         } while (randA==randB);        
  14.         [*array exchangeObjectAtIndex:randA withObjectAtIndex:randB];
  15.     }
  16. }
  17.  


quarta-feira, 14 de março de 2012

Dica - Saber quando a UITableView está sendo movimentada (Scrolling)

Pessoal,

Este código é para vocês saberem quando a UITableView está sendo movimentada (Scrolling). Ou seja, o cidadão começou a rolar a tabela para baixo e você quer ter os mesmos Delegates que o UIScrollView.

Sabendo que o UITableView herda de UIScrollView... segue o código:

  1. //In ViewController.h
  2. #import <UIKit/UIKit.h>
  3. @interfaceViewController:UIViewController<UIScrollViewDelegate>
  4. {
  5.     IBOutletUITableView* myTable;
  6.     IBOutletUIScrollView* myScroll;
  7. }
  8. //In ViewController.m
  9. #pragma mark -View lifecycle
  10. -(void)viewDidLoad{
  11.     [super viewDidLoad];
  12.     // Do any additional setup after loading the view, typically from a nib.
  13.     myScroll = myTable;
  14.     myScroll.delegate = self;
  15. }
  16. #pragmaMarkUIScrollViewDelegate
  17. -(void)scrollViewDidScroll:(UIScrollView*)scrollView{
  18.     NSLog(@"%f",scrollView.contentOffset.y);
  19. }


sexta-feira, 24 de fevereiro de 2012

Função - Pegar a versão do projeto

  1. //Retorna uma NSString com a versão que está o projeto.
  2. [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];




sábado, 10 de dezembro de 2011

Função - Formatar horário a partir de X segundos


//  ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
    // Criando um processo para chamar o métodos onTimer a cada 1 segundo
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

- (void)onTimer
{
    static int segundos = 0;
    
    segundos++;
        
    // Imprimi no console o horário formatado
    NSLog(@"%@",[self horarioFormatadaComSegundos:segundos]);
}

// Função para formatar um horário a partir de inteiros.
- (NSString *)horarioFormatadaComSegundos:(int)segundos
{
    int hor = 0, min = 0, seg = 0;
    
    seg = segundos;
    
    while (seg >= 60) { min+=1; seg-=60; }
    
    while (min >= 60) { hor+=1; min-=60; }
    
    NSString * h, * m, * s;
    
    h = (hor > 9) ? [NSString stringWithFormat:@"%d",hor] : [NSString stringWithFormat:@"0%d",hor] ;
    m = (min > 9) ? [NSString stringWithFormat:@"%d",min] : [NSString stringWithFormat:@"0%d",min] ;
    s = (seg > 9) ? [NSString stringWithFormat:@"%d",seg] : [NSString stringWithFormat:@"0%d",seg] ;
    
    return [NSString stringWithFormat:@"%@:%@:%@",h,m,s];
}


Cantos Arredondados nas Imagens, Botões, e etc.


Adicionar o seguinte framework ao projeto:



// Declarar no header
#import <QuartzCore/QuartzCore.h>



// Código
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Criando uma imagem
    CGRect rect         = CGRectMake((self.view.frame.size.width/2)-57, (self.view.frame.size.height/2)-90, 114, 175);
    UIImageView * image = [[UIImageView alloc] initWithFrame:rect];
    [image setImage:[UIImage imageNamed:@"bio.jpg"]];
    
    
    // Modificando os cantos
    image.layer.cornerRadius  = 20.0f;
    image.layer.masksToBounds = YES;
    
    
    // Adicionando a imagem na tela
    [self.view addSubview:image];
    
    [image release]; image = nil;
}

=================================================================================

Resultado:













Adicionando Sombra nas Imagens


Adicionar o seguinte framework ao projeto:



// Declarar no header
#import <QuartzCore/QuartzCore.h>



// Código
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Criando uma imagem
    CGRect rect         = CGRectMake(10, 20, 280, 428);
    UIImageView * image = [[UIImageView alloc] initWithFrame:rect];
    [image setImage:[UIImage imageNamed:@"bio.jpg"]];
    
    // Adicionando a Sombra
    image.layer.shadowColor   = [UIColor grayColor].CGColor;
    image.layer.shadowOffset  = CGSizeMake(2, -2);
    image.layer.shadowOpacity = 1;
    image.layer.shadowRadius  = 1.0;
    
    // Adicionando a imagem na tela
    [self.view addSubview:image];
    
    [image release]; image = nil;
}

=================================================================================

Resultado:













terça-feira, 29 de novembro de 2011

Função - Gerando números aleatórios


srand(time(NULL));                 // Sequência que nunca se repete
    
for (int i = 0; i < 10; i++)      
{
    int r = rand()%100;            // Gerando número aleatório de 0 à 100.
        
    NSLog(@"%d",r);                // Imprimindo resposta
}





srand(100);                        // Sequência que sempre se repete
    
for (int i = 0; i < 10; i++)     
{
    int r = rand()%100;            // Gerando número aleatório de 0 à 100.
        
    NSLog(@"%d",r);                // Imprimindo resposta
}