Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

objective c - How to add sound while UIScrollview is scrolling in iphone sdk?

This is my code for adding sound while UIScrollview is scrolling in iPhone SDK. In this code, how to add the sound while using touch-events in scrolling? Please give me your idea and suggestion or sample.

const CGFloat kScrollObjHeight  = 151.0;
const CGFloat kScrollObjWidth   =320.0;    
const NSUInteger kNumImages  = 5;

- (void)layoutScrollImages
{
  UIImageView *view = nil;
  NSArray *subviews = [scrollView1 subviews];
  CGFloat curXLoc = 0;
  for (view in subviews)
    {
      if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
       {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;
        curXLoc += (kScrollObjWidth);
       }
    }
   [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
 }

- (void)viewDidLoad
 {
   self.view.backgroundColor = [UIColor clearColor];
   [scrollView1 setBackgroundColor:[UIColor whiteColor]];
   [scrollView1 setCanCancelContentTouches:NO];
   scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   scrollView1.clipsToBounds = YES; 
   scrollView1.scrollEnabled = YES;
   scrollView1.pagingEnabled = YES;
   NSUInteger i;
   for (i = 1; i <= kNumImages; i++)
   {
     NSString *imageName = [NSString stringWithFormat:@"snap%d.jpg", i];
     UIImage *image = [UIImage imageNamed:imageName];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
     CGRect rect = imageView.frame;
     rect.size.height = kScrollObjHeight;
     rect.size.width = kScrollObjWidth;
     imageView.frame = rect;
     imageView.tag = i;
     [scrollView1 addSubview:imageView];
     [imageView release];
     [self layoutScrollImages];
   }
   [super viewDidLoad];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

add UIScrollViewDelegate to your interface and then play sound while the scrolling is in task in scrollViewWillBeginDragging state and stop scrolling while in scrollViewDidEndDragging state.

You can use AVAudioPlayer to play sound on iOS devices

UPDATE:

Tells the delegate when the scroll view is about to start scrolling the content.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

Also, Tells the delegate when dragging ended in the scroll view.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

Implement these in your code & put in your custom behavior


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...