2013-08-13 33 views
6

Đang làm việc với hiệu ứng lắc cho nhãn tôi gọi là hoạt ảnh trong khi nhấn ô trong bảng nhìn nhưng nó không hoạt động. Nếu tôi gọi hoạt ảnh trong cellForRowAtIndexPath: (NSIndexPath *) indexPath nó đang hoạt động nhưng nếu tôi cal nó trong didSelectRowAtIndexPath: (NSIndexPath *) indexPath nó không hoạt động. có ai giúp tôi không??? cảm ơn trướcTạo hiệu ứng lắc cho nhãn trong ios?

-(void)shakeAnimation:(UILabel*) label { 
    const int reset = 5; 
    const int maxShakes = 6; 

    //pass these as variables instead of statics or class variables if shaking two controls simultaneously 
    static int shakes = 0; 
    static int translate = reset; 

    [UIView animateWithDuration:0.09-(shakes*.01) // reduce duration every shake from .09 to .04 
          delay:0.01f//edge wait delay 
         options:(enum UIViewAnimationOptions) UIViewAnimationCurveEaseInOut 
        animations:^{label.transform = CGAffineTransformMakeTranslation(translate, 0);} 
        completion:^(BOOL finished){ 
         if(shakes < maxShakes){ 
          shakes++; 

          //throttle down movement 
          if (translate>0) 
           translate--; 

          //change direction 
          translate*=-1; 
          [self shakeAnimation:label]; 
         } else { 
          label.transform = CGAffineTransformIdentity; 
          shakes = 0;//ready for next time 
          translate = reset;//ready for next time 
          return; 
         } 
        }]; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [self shakeAnimation:cell.MyHomeMenuCountlabel]; 

} 
+2

Xác định "không hoạt động". Và bạn đang lấy 'ô' trong phương thức' didSelectRowAtIndexPath' của bạn ở đâu? – rmaddy

+0

@maddy không thể giúp bạn ??? –

+0

tôi đã tạo ô tùy chỉnh và chỉ áp dụng hoạt ảnh cho nó –

Trả lời

3

Có 2 w ays để làm điều này:

1.Try chức năng này:

NSArray *arrIndexPath = [NSArray arrayWithObject:indexPath]; 
[dropTable reloadRowsAtIndexPaths:arrIndexPath withRowAnimation:UITableViewRowAnimationFade]; 

chức năng didSelectRowAtIndexPath.

Chỉ thực hiện hoạt ảnh trong cellForRowAtIndexPath.

2.Gán thuộc tính thẻ cho ô của bạn trong cellForRowAtIndexPath.

cell.tag = indexPath.row. 

Trong didSelectRowAtIndexPath:

Sau đó, sử dụng subviews của bảng, có cell.tag bằng indexPath.row và lấy tài liệu tham khảo của cùng một tế bào và sau đó làm hoạt hình:

for (MyHomeViewCell *cell in [table subviews]) { // change name of table here 
    if ([cell isKindOfClass:[MyHomeViewCell class]]) { 
    if (cell.tag == indexPath.row) { 
     [self shakeAnimation:cell.MyHomeMenuCountlabel]; 
    } 
    } 
} 
+0

nó không hoạt động –

+0

thẻ không hoạt động –

+0

cách gán thẻ ??? –

2

Finlay i got you solution hy vọng điều này có thể giúp bạn. Đối với lắc Label đặc biệt mà bạn có thể thêm UITapGestureRecognizer để nhận đặc biệt UILabel vòi và thiết lập hình ảnh động Shake vào UITapGestureRecognizer 's @selector phương pháp như Bellow dụ: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(100, 7, 300, 30)]; 
    cellTitle.userInteractionEnabled = YES; 
    cellTitle.tag = indexPath.section; 
    [cellTitle setBackgroundColor:[UIColor clearColor]]; 
    [cellTitle setFont:[UIFont fontWithName:@"Helvetica" size:14]]; 
    [cellTitle setText:_objects[indexPath.row]]; 
    [cellTitle setTextColor:[UIColor blackColor]]; 
    [cell.contentView addSubview:cellTitle]; 


    UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] init]; 
    [labelTap addTarget:self action:@selector(viewPatient:)]; 
    [labelTap setDelegate:nil]; 
    [labelTap setNumberOfTapsRequired:1]; 
    [cellTitle addGestureRecognizer:labelTap]; // cellTitle add here 
    [labelTap release]; 
    return cell; 
} 



-(void)viewPatient:(id)sender 
{ 

    UILabel *lObjLabel = (UILabel *)[sender view]; 

    CGFloat t = 2.0; 
    CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0); 
    CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0); 

    lObjLabel.transform = translateLeft; 

    [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{ 

     [UIView setAnimationRepeatCount:10]; 
     lObjLabel.transform = translateRight; 
    } completion:^(BOOL finished) { 

     if (finished) { 
      [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
       lObjLabel.transform = CGAffineTransformIdentity; 
      } completion:NULL]; 


     } 
    }]; 
} 

Nó đang làm việc như: -

enter image description here

+2

Việc sử dụng các phương pháp hoạt ảnh này không được khuyến khích như iOS 4.0. Bạn thực sự nên sử dụng phương thức hoạt ảnh dựa trên khối thay thế. – rmaddy

+1

Tôi chỉ tự hỏi tại sao không ai muốn sử dụng hoạt ảnh dựa trên khối như Apple khuyến cáo và khuyến khích các nhà phát triển trên iOS4 trở lên? – holex

4

sử dụng mã này cho hoạt ảnh lắc cho chế độ xem

-(void)shakeAnimation:(UILabel*) label 
{ 
     CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"]; 
     [shake setDuration:0.1]; 
     [shake setRepeatCount:5]; 
     [shake setAutoreverses:YES]; 
     [shake setFromValue:[NSValue valueWithCGPoint: 
          CGPointMake(label.center.x - 5,label.center.y)]]; 
     [shake setToValue:[NSValue valueWithCGPoint: 
          CGPointMake(label.center.x + 5, label.center.y)]]; 
     [label.layer addAnimation:shake forKey:@"position"]; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell cell = [theTableView cellForRowAtIndexPath:theIndexPath]; 
UILabel label=(UILabel*)[cell.contentView viewWithTag:2001]; 
[self shakeAnimation:label]; 

} 
+0

nó hoạt động tốt nhưng tôi muốn lắc khi một hàng được chọn làm thế nào để làm điều đó ?? –

+0

bạn truy cập vào ô đã chọn và truy cập uilabel trong ô sử dụng thẻ như thế này UITableViewCell * cell = [theTableView cellForRowAtIndexPath: theIndexPath]; UILabel * label = (UILabel *) [cell.contentView viewWithTag: 2001]; [tự lắcHiệu ứng: nhãn]; – NANNAV

+0

tôi đã tạo ô tùy chỉnh có nhãn mỗi hàng sẽ có một nhãn –

0

sử dụng chức năng này - (void) shakeView: (UIView *) viewToShake và ghi lại bất kỳ mã hoạt ảnh nào bạn muốn trong chức năng này và gọi nó từ nơi bạn muốn tạo ảnh động cho mục của bạn

và nếu bạn chọn hình ảnh động đầu tiên sau đó cũng sử dụng đoạn mã sau mảnh quá chức năng này được sử dụng để kết thúc hoạt hình đúng

#define RADIANS(degrees) ((degrees * M_PI)/180.0) 

- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
UIView* item = (__bridge UIView *)context; 
item.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(90.0)); 
} 

và đây là hai loại hoạt hình đã chọn bất cứ điều gì bạn muốn


1. rung hoạt hình như xóa một trang từ ipad hoặc iphone và quyết định lắc bởi góc thích hợp

CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(88.0)); 
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(92.0)); 

viewToShake.transform = leftWobble; // starting point 

[UIView beginAnimations:@"wobble" context:(__bridge void *)(viewToShake)]; 
[UIView setAnimationRepeatAutoreverses:YES]; // important 
[UIView setAnimationRepeatCount:2000]; 
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)]; 

viewToShake.transform = rightWobble; // end here & auto-reverse 

[UIView commitAnimations]; 

2.normal rung động

CGFloat t = 2.0; 
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, 85, 0.0); 
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, 95, 0.0); 

viewToShake.transform = translateLeft; 

[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{ 
    [UIView setAnimationRepeatCount:2.0]; 
    viewToShake.transform = translateRight; 
} completion:^(BOOL finished) { 
    if (finished) { 
     [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
      viewToShake.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 95, 0.0); 
     } completion:NULL]; 
    } 
}]; 

}