滚动时淡出效果

时间:2014-05-18 19:46:18

标签: ios objective-c uiscrollview

当用户滚动从服务器获取数据的galleryViewController时,我想添加淡入淡出效果。我无法将淡入淡出效果实现到scrollViewController

代码:

#import "GrillGalleryCollectionViewController.h"
#import "AFNetworking.h"
@interface GrillGalleryCollectionViewController ()

@end

@implementation GrillGalleryCollectionViewController
@synthesize scrollView,pageControl, colors;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 //   colors=[[NSArray alloc]init];
    colors = [[NSArray alloc] init];;

    [self getActiveOffers];
   // NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];

}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)changePage:(id)sender {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}

- (IBAction)backBtnPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void) getActiveOffers {

    NSString *string = @"http://znadesign.com/appcenter/api.php?function=get_gallery&customer_id=1";
    NSLog(@"%@", string);
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        int total_count = (int)[[responseObject valueForKey:@"total_count"] integerValue];
        if (total_count > 0) {
            NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:total_count];
            for (int i = 1; i <= total_count; i++) {
                NSString *key = [NSString stringWithFormat:@"%i", i];
                id object = [responseObject objectForKey:key];
                [array addObject:object];
            }
            colors = [NSArray arrayWithArray:array];
            [self setSizeSliding];
           // [myTableView reloadData];
        }
        else
        {
            UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:@"There is no internet connection."
                                                                message:nil
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alertView2 show];
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"There is no internet connection."
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    // 5
    [operation start];

}

-(void) setSizeSliding
{
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        //  UIView *subview = [[UIView alloc] initWithFrame:frame];
        //  subview.backgroundColor = [colors objectAtIndex:i];
        //   NSString *imageURLString=[[offersArray objectAtIndex:indexPath.row] valueForKey:@"picture"];

        NSString*slidingImage = [[colors objectAtIndex:i] valueForKey:@"picture"];
        NSURL *url = [NSURL URLWithString:slidingImage];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage *tmpImage = [[UIImage alloc] initWithData:data];

        UIImageView *slidingImageView = [[UIImageView alloc]initWithFrame:frame];
        slidingImageView.image = tmpImage;
        [self.scrollView addSubview:slidingImageView];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);


}
@end

我想实现类似的淡入淡出效果,如下所示:

  [UIView animateWithDuration:2
                         animations:^{imageView.alpha = 0.0;}
                         completion:^(BOOL finished){ [imageView removeFromSuperview];}];

1 个答案:

答案 0 :(得分:0)

您有代码可以添加以下代码并尝试动画是否有效..

-(void) animateSubViews
{
    int buffer = 10;
    CGRect frame = CGRectMake((self.pageControl.currentPage * self.scrollView.frame.size.width)-buffer, 0, self.scrollView.frame.size.width + buffer, self.scrollView.frame.size.height);
   [UIView animateWithDuration:0.4 
                     animations:^{
                          for (UIView *view in self.scrollView.subviews)
                          {
                             if (CGRectContainsRect(frame, view.frame))
                             {
                                [view setAlpha:1.0];
                             }
                             else
                             {
                                [view setAlpha:0.0];
                             }
                          }
                    }];
}

尝试从ScrollView调用此方法滚动并更改页面。

- (IBAction)changePage:(id)sender {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];

   //Call to animate
   [self animateSubViews];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;

   //Call to animate
   [self animateSubViews];
}